Followers

Monday, May 22, 2023

Variable declaration in C language.


In C, a variable is a named storage location that holds a value. It is used to store and manipulate data within a program. Variables have a specific type that determines the range of values it can hold and the operations that can be performed on it.

Syntax for declaring a variable in C:

C
data_type variable_name;

Here, data_type represents the type of the variable, and variable_name is the name given to the variable. For example, to declare an integer variable named age, you would use the following syntax:

C
int age;

We can also initialize a variable at the time of declaration by assigning a value to it:

C
data_type variable_name = value;

Here's an example of declaring and initializing a few variables in C:

C
#include <stdio.h> 
int main()
{
int age = 25;
float height = 1.75;
char grade = 'A';
printf("Age: %d\n", age);
printf("Height: %.2f\n", height);
printf("Grade: %c\n", grade);
return 0;
}

In this example, three variables are declared and initialized: age of type int, height of type float, and grade of type char. The values are then printed using printf statements.

Output:

C
Age: 25 Height: 1.75 Grade: A

Variables can be modified by assigning new values to them throughout the program, allowing for dynamic storage and manipulation of data. 

No comments:

Post a Comment