What is a variable?
A variable is a named storage location whose value can change during programming.
Each variable in C language has a specific data type, which determines the size and layout of the variable in memory, the range of values that can be stored within that memory and the set of operations that can be performed on the variable.
Rules for naming a variable:
The name of a variable can be composed of letters, digits, and the underscore character. It must begin with either a letter or an underscore character. Upper and lowercase alphabets are allowed and are distinct because C is case-sensitive.
What is Data Type?
Data type helps us to specify the type of data/value that we want to store in a memory location.
It may be a numeric value without decimals or numeric value with decimals or alphanumeric data. To store such values we have data types like:
int
float
double
char
Data Type | Details |
int | A variable of type int can store a numeric value without decimal (without fraction). Size : 2 byte Range : -32768 to 32767 Format specifier: %d or %i Example : int a,b,c; |
float | A variable of type float can store a numeric value with decimal. Size : 4 byte Range : 3.4e-38 to 3.4e+38 ( 3.4 * 10-38 to 3.4 * 10+38 ) Format specifier: %f Example : float x,y,z; |
double | A variable of type double can store a numeric value with decimal. Size: 8 byte Range : 1.7e-308 to 1.7e+308 Format specifier: %lf Example: double a1,a2; The range of double is much larger as compared to float. |
char | A variable of type char can store alphanumeric value. Size: 1 byte Range : -128 to 127 ( 0 to 255 ) can be used in two ways (i). char n; can store only a single character Format specifier: %c (ii). char n[20];(20: represents the size, can be anything as per requirement) can store a group of characters Format specifier: %s This is known as a character array or string. |
Variable Definition in C
A variable definition tells the compiler
How much storage to create for the variable
Where the memory is created for the variable
A variable definition also specifies a data type of variable.
datatype variable_list;
Here,
* datatype must be a valid C data type including char, w_char, int, float, double, bool, or any user-defined object
* variable_list may consist of one or more variable names/identifier names separated by commas.
Some valid declarations are shown here −
int a,b,c;
int abc,xyz,pqr;
char name[10], ch;
float bal salary;
double d;
Line 1:
int a,b,c;
declares and defines the variables a, b, and c
which instructs the compiler to create variables named a, b and c of type int.
Line 2:
float bal,salary;
declares and defines the variables bal, salary
which instructs the compiler to create variables named bal and salary of type float.
Variable can be initialized in two ways:
* At the time of declaration
example:
int a=10,b=20,c=30;
float sal=45000,bal=25000;
* After declaration
int a,b,c;
float sal,bal;
a=10;
b=20
c=30
sal=45000;
bal=25000;
Variable Declaration in C
A variable declaration indicates to the compiler that there exists a variable with the given data 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.
A variable declaration is useful when you are using multiple files and you define your variable in one of the files which will be available at the time of linking of the program. You will use the keyword extern to declare a variable at any place. Though you can declare variable multiple times in your C program, it can be defined only once in a file, a function, or a block of code.