Variables in C language is topic for today. Here you will know variable declaration rules in C.
Moreover, you can check variables in C examples. However, remember to understand rules first.
These are rules for declaring variables in C.
What is variable in C?
Variables in C definition is simple. Because variable is name for storage area. Here program will
manipulate. In addition, variables in C have type. This type tells you layout and size of memory of
variable. You may even store range of values. Finally applying operations to variable is easy.
Name of variables in C language
Letters, digits and underscore makes variables names. You must know that C is case sensitive
programming language. Therefore, upper and lowercase characters have different meanings.
However, name of variable must start with letter or underscore.
Name of the Type | Description |
float | This is single-precision floating point value |
void | It will represent absence of type |
char | It is single octet that is one byte. In addition, this is integer type |
double | This type is double-precision floating point value |
int | Finally, this type is natural size of integer for machine |
Know variable definition in C programming language
Variable definition will tell compiler how much or where should they create storage. It also
indicates Data type in C. In addition, there will be one or more than one variable in this list.
Program example
Even initialization of variables is possible. It happens during declaration. You must remember that
initializer will have equal sign. Moreover, after this there will be constant expression.
Example program
In fact if there is definition without initializer. Then variables, which have static storage duration, will
initialize with NULL. This is initial value for all undefined variables.
How to declare variable in C
If there is variable declaration then compiler is happy. Because compiler can go forward with the
process. Moreover, compiler requires variable declaration when linking program. It is also useful
when you use multiple files. Even you can define variables.
However, you must not forget to use ‘extern’ keyword. As this declares variable at other places. Finally, declaration of variables in C may happen multiple times. But you can define it only once. This happens once in file, block of code or function.
Example program
Rvalues and Lvalues in C language
In C programming language, you will come across two expressions. These are lvalue and the rvalue.
Lvalue are expressions referring to memory location. This may appear at right or left hand side of assignment. On the other hand, there is rvalue. Rvalue is for data value stored at address in memory. In addition, rvalue can never have value assigned to it. Hence, rvalue is on right hand side of assignment only. And it can never come on left hand side.
Points to remember
- Lvalues are variables
- That is why they may appear on left hand side of assignment
- Whereas numeric literals are rvalues
- Therefore assignment of these values may not happen
- Hence they will not come on left hand side
Program example
So, now you know variables declaration rules in C. However, do not forget to practise variable declaration in C example programs. This way you will know about C variables declaration methods. Moreover these relate to Constants in C also. To know how read next article.
No comments:
Post a Comment