Showing posts with label Variables and Constants. Show all posts
Showing posts with label Variables and Constants. Show all posts

Sunday, 8 January 2012

Constants

Summary: in this tutorial, you will learn how use C constants in your code to make your code more readable and easy to maintain.
Introduction to C constants
C constants using C preprocessor
There are a lot of cases you want to use a constant in your code for example to calculate the square of a circle you code as follows:
square = 3.14159 * diameter;
In the statement, the constant 3.14159 represents the constant PI. Instead of putting constant in literal form, we can use symbolic constant as below:
square = PI * diameter;
As you see, the C constants make your code readable and the symbolic constants tell you not only the value but also its meaning. In addition, if a constant is used in several places, it is better to use symbolic constant because when you want to change its value you just have to change it in one place that define the constant, see the below example:
1
total =  net + TAXRATE * net;
Later on if the tax rate changes, you only have to change it in where it is defined.But how do you define symbolic C constants? The first idea of defining a C constant is using C preprocessor as follows:
#define PI 3.14159
#define TAXRATE 0.1
By doing this, when your program compiled, the constant values 1.14159 and 0.1 will be substituted everywhere you used PI and TAXRATE. This feature is known as compile time substitution. Those defined constants are called manifest constants because when you run the program, all the substitutions have already been made.
Note that there is no equal sign between constant’s name and value and there is no semicolon (;) at the end of the statement.
It is highly recommended that you use constant name is upper case so when you see it in code you know that it is a constant. The formula of defining a constant is as follows:
#define CONSTANT_NAME value
C constants using const keyword
C90 officially added a second way to create symbolic constants using the constkeyword to declare a constant. This approach is more flexible that using C preprocessor #define such as you can use keyword const to declare an array, pointer…etc.Once the value of a constant using const keyword has been initialized, its value cannot be changed through the program. See the below example:
const double PI = 3.14159;
const double TAXRATE = 0.1;

Thursday, 5 January 2012

Variables

A variable is an entity that may change it value. In any program we typically do lots of calculations. The results of these calculations are stored in computer memory locations. To make the retrieval and usage of these values we give names to the memory locations. These names are called variables.

A variable is just a named area of storage that can hold a single value (numeric or character). 
The C language demands that you declare the name of each variable that you are going to use and its type, or class, before you actually try to do anything with it.
The Programming language C has two main variable types
Local Variables
Global Variables
Local Variables
Local variables scope is confined within the block or function where it is defined. 
Local variables must always be defined at the top of a block.
When a local variable is defined - it is not initalised by the system, you must initalise it yourself.
When execution of the block starts the variable is available, and when the block ends the variable 'dies'.
Check following example's output
   main()
   {
      int i=4;
      int j=10;
      i++;
      if (j > 0)
      { 
         /* i defined in 'main' can be seen */
         printf("i is %d\n",i); 
      }
      if (j > 0)
      {
         /* 'i' is defined and so local to this block */
         int i=100; 
         printf("i is %d\n",i);      
      }/* 'i' (value 100) dies here */
      printf("i is %d\n",i); /* 'i' (value 5) is now visable.*/
   }
      This will generate following output
   i is 5
   i is 100
   i is 5
Here ++ is called incremental operator and it increase the value of any integer variable by 1. 
Thus i++ is equivalent to i = i + 1;
You will see -- operator also which is called decremental operator and it decrease the value of any integer variable by 1. 
Thus i-- is equivalent to i = i - 1;
Global Variables
Global variable is defined at the top of the program file and it can be visible and modified by any function that may reference it.
Global variables are initalised automatically by the system when you define them!
Data Type Initialser
int  0
char '\0'
float  0
pointer NULL
If same variable name is being used for global and local variable then local variable takes preference in its scope. 
But it is not a good practice to use global variables and local variables with the same name.
   int i=4;          /* Global definition   */
   main()
   {
       i++;          /* Global variable     */
       func();
       printf( "Value of i = %d -- main function\n", i );
   }
   func()
   {
       int i=10;     /* Local definition */
       i++;          /* Local variable    */
       printf( "Value of i = %d -- func() function\n", i );
   }
   This will produce following result
   Value of i = 11 -- func() function
   Value of i = 5 -- main function
i in main function is global and will be incremented to 5. 
i in func is internal and will be incremented to 11. 
When control returns to main the internal variable will die and and any reference to i will be to the global.+
host gator coupon