A variable is a value that may change during the execution of a program.
C language has specified some rules which should be followed in order to give a variable name in a C program.
These rules are as follows :
A variable name must start with an alphabet.
In general, the total length of a valid variable name could be between 1 to 8, though some compiler may allow larger length i.e length until 255 characters.
A variable name must not contain a space or a comma.
A variable name may contain an underscore.
Let's see an example of how we can declare variable in a program :
int size = 1000;
float ink_prc = 9.50;
short dist2 = 20;
char alpha_1 = 'a';
printf("The value in variable size is : %d \n", size);
printf("The value in variable inc_prc is : %f \n", ink_prc);
printf("The value in variable dist2 is : %d \n", dist2);
printf("The value in alpha_1 is : %c", alpha_1);
The value in variable size is : 1000
The value in variable inc_prc is : 9.500000
The value in variable dist2 is : 20
The value in alpha_1 is : a
Keywords in C
Keywords are the special words whose meaning is already defined in C programming language.
Keywords in C are also known as reserved words because name of any keyword cannot be used for a variable name in a C program.
There are about 32 keywords defined in C language.
Let's see a table of these keywords defined in C language.
Constant is a value that remains the same and does not change or it cannot be
modified. C language allows us to define constants in a program, using the
keyword const and the value of such constant does not change throughout
the course of execution of the program, even when one intentionally tries
to modify it. Let's look at an example to define a constant.
/* declaring a constant in C */
const int distance = 10;
printf("The value of constant distance is : %d", distance);
The value of constant distance is : 10
Trying to modify the a constant value in C
Let's try to modify a constant value declared with const keyword in a C program named Cons.c and see what happens.
/* Trying to modify a constant in C */
const int kg = 1000;
kg = 2000;
printf("The value of kg is : %d", kg);
Cons.c: In function 'main':
Cons.c:8:5: error: assignment of read-only variable 'kg'
kg = 2000;
As you can see in the output of the last program, the compiler has thrown an error stating that because kg is a
declared at a constant using const keyword, that's why its value is just read-only and cannot be modified.