Variables are memory locations(storage areas) in the C programming language.
The primary purpose of variables is to store data in memory for later use. Unlike constants which do not change during the program execution, the value of a variable may change during execution. If you declare a variable in C, that means you are asking the operating system to reserve a piece of memory with that variable name.
Variable Declaration in C
Syntax:
type variable_name;
or
type variable_name, variable_name, variable_name;
Variable Declaration and Initialization
Example:
int width, height=5;
char letter='A';
float age, area;
double d;
/* actual initialization */
width = 10;
age = 26.5;
Variable Assignment
A variable assignment is the process of assigning a value to a variable.
Example:
int width = 60;
int age = 31;
There are some rules on choosing variable names
- A variable name can consist of Capital letters A-Z, lowercase letters a-z, digits 0-9, and the underscore character.
- The first character must be a letter or underscore.
- Blank spaces cannot be used in variable names.
- Special characters like #, and $ are not allowed.
- C keywords cannot be used as variable names.
- Variable names are case-sensitive.
- Values of the variables can be numeric or alphabetic.
- Variable type can be char, int, float, double, or void.
C Program to Print Value of a Variable
Example:
#include<stdio.h>
void main()
{
int age = 33; // c program to print value of a variable
printf("I am %d years old.\n", age);
}
Program Output:
I am 33 years old.