Skip to main content

Variables in C Programming

What is a Variable

A variable is a named memory location that can hold a value of a specific data type. It is a way to temporarily store data in a program that can be accessed and modified as needed during runtime.

To declare a variable in C, you need to specify the data type and the name of the variable.

For example:

int age;
char gender;
float height;

Here, age is an integer variable, gender is a character variable, and height is a floating-point variable.

You can also assign a value to a variable when you declare it by using the assignment operator (=).

For example:

int age = 25;
char gender = 'M';
float height = 5.9;

Once a variable is declared, you can use it in your program by referencing its name. You can also change the value of the variable by using the assignment operator.

Types of Variables in C

There are several types of variables you can use, depending on the data type and the size of the value you want to store.

Here are some common types of variables in C:

  • char: This data type is used to store a single character, such as a letter, a number, or a symbol. It is usually stored in a single byte of memory and can hold values from -128 to 127.

  • int: This data type is used to store integer values. The size of an int variable depends on the system it is running on, but it is usually 2 or 4 bytes.

  • float: This data type is used to store floating-point values with decimal places. It is stored in 4 bytes of memory and can represent values from approximately 3.4 x 10^-38 to 3.4 x 10^38.

  • double: This data type is similar to float, but it is stored in 8 bytes of memory and can represent a wider range of values, from approximately 1.7 x 10^-308 to 1.7 x 10^308.

  • short: This data type is used to store smaller integer values, usually in 2 bytes of memory. It can hold values from -32768 to 32767.

  • long: This data type is used to store larger integer values, usually in 4 bytes of memory. It can hold values from -2147483648 to 2147483647.

  • long long: This data type is similar to long, but it is stored in 8 bytes of memory and can hold even larger integer values, from -9223372036854775808 to 9223372036854775807.

  • signed and unsigned: These are modifier keywords that can be used to change the range of values that an integer variable can hold.

Example showing Types of Variables

Following example shows the use of different types of variables:

#include <stdio.h>
#include <limits.h>

int main(void)
{
// Declare variables of different data types
char c = 'A';
int i = -100;
float f = 3.14;
double d = 3.14159265358979323846;
short s = 200;
long l = 1000000;
long long ll = 1234567890;
unsigned int ui = 100;

// Print the values and sizes of the variables
printf("char c = %c, size = %ld\n", c, sizeof(c));
printf("int i = %d, size = %ld\n", i, sizeof(i));
printf("float f = %f, size = %ld\n", f, sizeof(f));
printf("double d = %lf, size = %ld\n", d, sizeof(d));
printf("short s = %hd, size = %ld\n", s, sizeof(s));
printf("long l = %ld, size = %ld\n", l, sizeof(l));
printf("long long ll = %lld, size = %ld\n", ll, sizeof(ll));
printf("unsigned int ui = %u, size = %ld\n", ui, sizeof(ui));

return 0;
}
Output:

Output:

char c = A, size = 1
int i = -100, size = 4
float f = 3.140000, size = 4
double d = 3.141593, size = 8
short s = 200, size = 2
long l = 1000000, size = 4
long long ll = 1234567890, size = 8
unsigned int ui = 100, size = 4
sizeof operator

The sizeof operator is used to determine the size of a variable in bytes. As you can see, the size of each variable depends on its data type. For example, an int variable is usually stored in 4 bytes of memory, while a char variable is stored in just 1 byte.