In C, a variable is a named storage location in memory that holds a value of a particular data type. You can use variables to store data that your program needs to operate on or to keep track of intermediate results.
To declare a variable in C, you need to specify its data type and a name for the variable. Here's an example of declaring an integer variable named x
:
int x;
In this example, int
is the data type of the variable and x
is the name of the variable. You can also assign an initial value to the variable when you declare it.
int x = 42;
This declares an integer variable named x
and initializes its value to 42
. You can change the value of a variable later in your program by assigning a new value to it.
C supports several built-in data types, including:
int
: for integersfloat
anddouble
: for floating-point numbers (decimal numbers)char
: for individual charactersbool
: for Boolean values (true
orfalse
)
Here's an example program that demonstrates the use of variables in C:
#include <stdio.h>
int main() {
int x = 10, y = 20;
float z = 3.14;
char c = 'A';
bool flag = true;
printf("x = %d, y = %d\n", x, y);
printf("z = %f\n", z);
printf("c = %c\n", c);
printf("flag = %d\n", flag);
return 0;
}
In this program, we're declaring several variables of different data types and assigning them initial values. We're then outputting the values of the variables to the console using the printf
function and format specifiers.
Comments