What is variable in C?
In C, a variable is a user defined name that is used as a container to store data values.
Variables are used to store and manipulate data in C program.
The data type of a variable in C needs to be specified explicitly (data type must be declared before variable name).
Declaring Variable in C
1. Declaring Variable without Initialization:
In C, variable can be declared without initialization (without assigning value) and value can be assigned to the variable later.
Variable Syntax Without Initialization
xxxxxxxxxx
datatype variable_name;
datatype :
It specifies the data type of the variable to be declared. The 'datatype' will be replaced by the specified data type like int, float, char, etc.
variable_name :
It refers to a suitable name of a variable. The 'variable_name' will be replaced by a suitable variable name.
Example of Variable Without Initialization
xxxxxxxxxx
int my_number;
Initialization of Variable
xxxxxxxxxx
int my_number;
my_number = 25;
In the example, the code int my_number;
declares an integer variable named my_number
without assigning any value. The variable will be initialized later.
The code my_number = 25;
initializes the variable my_number
by assigning an integer value 42
.
Different components of the variable in the example:
int
is the data type.my_number
is the variable name.25
is the value assigned to the variable.
2. Variable Syntax with Initialization:
In C, variables can be declared and initialized (assigning value) with value at the time of declaration.
Variable Syntax With Initialization
xxxxxxxxxx
datatype variable_name = value;
datatype :
It specifies the data type of the variable to be declared. The 'datatype' will be replaced by the specified data type like int, float, char, etc.
variable_name :
It refers to a suitable name of a variable. The 'variable_name' will be replaced by a suitable name that you prefers for your variable.
value :
This refers to the data value to be assigned to the variable at the time of variable declaration. The 'value' will be replaced by your data value.
Example of Variable With Initialization
xxxxxxxxxx
float my_float_num = 12.56;
In the example, the code float my_float_num = 12.56;
declares a float (floating-point) data type variable named my_float_num
and initializes the variable by assigning a floating-point value 12.56
.
Different components of the variable in the example:
float
is the data type.my_float_num
is the variable name.12.56
is the value assigned to the variable.
3. Multiple Variables in Single Statement:
In C, multiple variables of the same type (data type) can be declared in one statement.
Example of Multiple Variables in Single Statement
xxxxxxxxxx
int x, y, z; //declares multiple variables without initialization
x = 12; //assigns value
y = 3;
z = 7;
int a = 5, b = 10, c = 15; //declares multiple variables with initialization
In the example, the code int x, y, z;
declares multiple variables in a single statement without initialization of the variables. These variables are initialized later. While the code int a = 5, b = 10, c = 15;
declares multiple variables in a single statement with initialization of the variables.
More Examples of Declaring Variables in C
xxxxxxxxxx
#include <stdio.h>
int main() {
//Variable without initialization
int integerVar; //Declares an integer variable
integerVar = 42; //Assigns a value to the variable
printf("Integer: %d\n", integerVar); // \n is the newline in c
// Variable with initialization
float floatVar = 3.14; //Declare and initialize a float variable
printf("Float: %f\n", floatVar);
int x, y;
x = 10;
printf("Integer x: %d\n", x);
y = 12;
printf("Integer y: %d\n", y);
return 0;
}
Output of the above example
Integer: 42 Float: 3.140000 Integer x: 10 Integer y: 12
%d and %f are formatters in C. Please read How to use formatters in C