Variables

Variables allow us to store information, perform calculations and operations on them. They are the named memory locations to store information temporarily. Before storing value in a variable, you need to declare it first using a data type.

Declaration of Variable

int age = 21;

Here, we are declaring an integer variable age using a data type called int and initializing it with value 21. Since we declared this variable with int, it now only allows integer values in this variable.

It is a best practice to initialize a variable as soon as you declare, as non-initialized variables contain undefined value, and if you accidentally use those non-initialized variable, your program may not behave as expected.

Printing the value of Variable

To print the value of the variable we can use the printf function which we saw in our previous section.

printf("Your age is %d", age);

As we want to print the value of the variable, we are passing the age variable as additional argument to printf function. The first argument is the text that gets printed on the screen.

If you observe we’ve written %d at the end of the text. We call it a format specifier. Using this format specifier, we are telling the program to print the variable value as a decimal value on the screen.

Variable Example

#include <stdio.h>

void main() {
    int age = 21;
    printf("Your age is %d", age);
}

Output:

Your age is 21

Where to declare the variables?

The new C compilers allow you to declare the variables wherever you need them, which was not the case with older compilers. Old C compilers require declaring all variables right after you open the curly bracket of a function. Below code works perfectly fine with new C compilers.

#include <stdio.h>

void main() {
    int age = 21;
    printf("Your age is %d", age);

    int yearOfBirth = 2000;
    printf("Year of Birth is %d", yearOfBirth);
}

Declaring multiple variables in a single line

You can declare multiple variables in a single line and initialize them at the same time as follows:

#include <stdio.h>

void main()
{
    int a = 10, b = 20;

    printf("a = %d, b = %d", a, b);
}

But it is a good practice to declare each variable in a new line for better readability.

Changing the value in a variable

Value in a variable can be changed and that is the reason why they are called variables.

#include <stdio.h>

void main()
{
    int n1 = 10, n2 = 20, sum = 0;
    sum = n1 + n2;
    printf("%d + %d = %d", n1, n2, sum);
}

In this program, sum is initialized as 0 when it was declared, but later we are modifying its value to the sum of both n1 and n2.

10 + 20 = 30

Variable Naming Conventions

  • First character of a variable must be an alphabet or underscore
  • Variable declaration doesn’t allow special characters and blanks other than underscore
  • We cannot use C language keywords as a variable name. Keywords are the reserved words that define the semantics of the language.

Below are the valid variable names:

int a123 = 10;
int _123 = 20;
int emp_id = 101;

Below are some invalid variable names:

int int = 10;
int void = 10;
int 123 = 10;
int priceIn$ = 10;