c constant and variables

By
Advertisement
Variables are named memory location or a name given to the memory location. Variables are basic data items to handle in your program. A variable must be declared and initialized before they can be used in your c program.


Variable Declaration

To declare a variable, specify the data type of the variable followed by its name. The data type indicates the kind of data type indicates the kind of data that the variable will store. Variable names should always be meaningful and must reflect the purpose of their usage in the program. The memory location of the variable is of importance to the compiler only and not to the programmer. In C, Variable declaration always ends with a semicolon, for example:
  •   Int emp_ num; 
  •    float salary;
  • Char grade; 
  • double balance _ amount;
C allows multiple variables of the same type to be declared in one statement. So the following statement is absolutely legal in C.

    Float  temp_in _ Celsius, temp _ in _ fahrengeit;

Variable Initialization:

                                  A variable can be defined at the time of declaration. Like as follows
int a=10;

A variable may also be initialized dynamically as follows

int a,b,c;
c=a+b;

Here the variable c is initialized at run time.

Constant in C

                      A constant in c is variable those value can't be changed throw out the program. A constant can be declared as follows

Declaring Constants: 

To declare a constant, precede the normal variable declaration with const  key word and assign it value. For example,
    const float pi= 3.14;
The const keyword specifies that the value of pi cannot change.

However, another way to designate a constant is to use the pre-processor command define. Like other pre-processor commands, it Is preceded with a # symbol. Although # define statements can be placed anywhere in a C program, it is always recommended that these statements be placed at the beginning of the program. For example,
#define PI 3.14159
# define service _ tax 0.12

Distinguish the difference between variables and constants

  • Variable is the named memory location whose value or you can say whose data can be change during execution of program. Where as Constant is the named memory location who value can’t be changed or whose value is fixed during the execution the program.

  • A variable is allocation in a memory which holds a piece of data, it can be represented by anything apart from numbers. Where as a constant can be any numbers or characters.

  • Variables can be initialize after its declaration where as constants must be initialized at the time of there deceleration otherwise they will take a garbage/junk value. 

  • Variable’s syntax is : datatype variable_name; and Constant’s syntax is : const datatype constant_name =value;

  • Variable’s example : int a = 10; and Constant’ example is : cons tint a =1 0;

0 comments:

Post a Comment