In simple words, variable is a name given to memory box with a name, where we can “store” some value. Its value can be changed depending upon conditions and it can be reused many times.
- A variable can support different type of data such as integer, float, character etc
- Value of variables can be changed according to the information passed to the program.
- You can perform different set of operation to the variables.
Rules for naming C Variable
- A Variable name must begin with letter or underscore.
- Variables are case sensitive
- You can use letters & digits in variable names.
- No special symbols can be used other than underscore.
- sum, city, person_2, _value are some examples for Variable name
Characters allowed in C variable name:
You can use Underscore(_), Capital Letters ( A – Z ), Small Letters ( a – z ) and Digits ( 0 – 9 ) while choosing a variable name. Blanks, Commas and other special symbols are not allowed in variable naming. Variable name Should not be Reserved Word.
Example of Valid or Invalid Variable Names
These are some of the examples of valid variable names for city
Invalid Variable Names
- city 1
Declaring & initializing C Variable
- First, you need to declare a variable in the C program before to use.
- Memory space is not created for a variable during declaration. It happens only on variable definition.
- Variable initialization assigs a value to the variable.
|1.||Variable declaration||data_type variable_name;||int x, y, z; char ch;|
|2.||Variable initialization||data_type variable_name = value;||int x = 10, y = 20; ch=’l’;|
Example of Variable in C Programming
Here is the simple example of C Variable:
/* Variable declaration */
/* Variable initialization */
num = 10;
/* print value of a variable */
printf("Number is: %d \n", num);