Variable declaration in C
A variable declaration provides assurance to the compiler that there exists a variable with the given type and name so that the compiler can proceed for further compilation without requiring the complete detail about the variable. A variable definition has its meaning at the time of compilation only, the compiler needs actual variable definition at the time of linking the program.
#include <stdio.h>
extern int a, b;
extern int c;
extern float f;
int main ( )
{
int a , b ;
int c ;
float f ;
a = 10 ;
b = 20 ;
c = a + b ;
printf("value of c : %d \n",c);
f = 70.0/3.0;
printf ("value of f:%f \n",f);
return 0;
}
Output:
value of c: 30
value of f: 23.333333
Rules for writing the variable name in C
A variable declaration provides assurance to the compiler that there exists a variable with the given type and name so that the compiler can proceed for further compilation without requiring the complete detail about the variable. A variable definition has its meaning at the time of compilation only, the compiler needs actual variable definition at the time of linking the program.
#include <stdio.h>
extern int a, b;
extern int c;
extern float f;
int main ( )
{
int a , b ;
int c ;
float f ;
a = 10 ;
b = 20 ;
c = a + b ;
printf("value of c : %d \n",c);
f = 70.0/3.0;
printf ("value of f:%f \n",f);
return 0;
}
Output:
value of c: 30
value of f: 23.333333
Rules for writing the variable name in C
- A variable name can have letters (both uppercase and lowercase letters), digits and underscore only.
- The first letter of a variable should be either a letter or an underscore. However, it is discouraged to start the variable name with an underscore. It is because the variable name that starts with an underscore can conflict with a system name and may cause an error.
- There is no rule on how long a variable can be. However, the first 31 characters of a variable are discriminated by the compiler. So, the first 31 letters of two variables in a program should be different.
Tags:
Programming in C