C Variables

What’s are Variables in C?

In the C programming language, C variables are much like algebraic variables that represent/hold any unknown or variable values. In general, variables are used in C programming to hold some specific type of data temporarily. The value of a variable can be changed easily by overriding value.

Variables are a basic concept of most high-level and mid-level programming language. And the purpose of using variables in all languages are the same, that is storing temporary value in it. But the declaring procedure, and implementing method of variables can vary language to language. Read below to learn all the things about variable in C.

Naming Variables in C

Before naming a variable in C, you should learn the basic rules of naming variable. Here you go:

  • While naming a variable in C, you can only use, Letter (A-Z | a-z), Numbers (0-9), and underscore only.
  • The first character must be a Letter or underscore; must not be a Number (0-9)
  • Any other special characters except underscore are not allowed to use in the variable name
  • Variables name must not be a reserved keyword like return, double, etc. See Complete reserved Keyword list in C

Here is how you can name a variable by following those four simple rules. Now it is time to declare the variable name in our C programming source code.

Declaring Variables in C

C is a statically typed language. It means the memory is not allocated for storing data in C programming. We must declare the variable before using it. Moreover, you have to give some more information about the variable before using it to the compiler.

The declaration statement also includes the data type you want to store. This means you have to tell the compiler which data you are going to store.

Example Fig1.1;

int students;
// here "students" is the declared variable
//int is a keyword that tells the compiler to store integer number

So if we generalize the variable declaration, we get;

//single variable declaration
type variable_name;

//multiple variables declaration
type variable_name1, variable_name2, variable_name3, ....;

In C programming you can store a total of four kinds of data in a variable;

char ex: char ch; Typically store character data, this data type takes 1 byte from the memory space
int ex: int x, y, z; It stores integer number (0-9), it takes 4 bytes on a 32/64 bit computer
float ex: float x, y; It stores number with fractional part call real number. It also takes 4 bytes from the computer memory.
double ex: double is the same as float, but it is big in size. It takes 8 bytes from memory for storing data.

We will discuss the data types in the later chapter in details.

Example Fig1.2:

#include <stdio.h>

int main() {
    int x, y, sum;
    float a, b, sum;
    double t, q, temp;
    char ch;

    return 0;

Initializing the value of variables

You can initialize or assign the values to a variable in two ways. First one is, initializing or assigning the value while declaring the variable and another one is, defining the variable with no value first and then initializing the value after defining the variables. From example figure 1.2;

Example Fig1.3:

#include <stdio.h> 

int main() {
    //Initializing the variable while declaration
    int x = 4, y = 8, sum = x + y;
    float a = 5.90, b = 2.30, sub = a - b;
    double t = 130.230, q = 4.95, temp = t/q;
    char ch ='Abc';

    //Initializing the variable after the variable definition
    int x, y, sum;
    float a, b, sub;
    double x, y, temp;
    char ch;

    x = 4;
    y = 8;
    sum = x + y;
    a = 5.90;
    b = 2.30
    t = 130.230;
    q = 4.95;
    temp = t / q
    ch = 'Abc'

    return 0;

Variables are Case Sensitive

C is a case sensitive language. Variables declared in the same name one with lower case and another one is uppercase is not the same variable.

For example, int number; int Number; and int NUMBER; are three different variables.

Using the Variable naming Conventions

While declaring a variable we should follow the variable naming convention. Convention makes our code standard and easy to read & understand.

The following conventions generally make the source code standard;

  • Start variable name with a lowercase letter (eg: numbers is conventional, not Numbers)
  • Separate two words using underscore or an uppercase letter. (eg: first_name or firstName is conventional, not FirstName, firstnane)
  • Use meaningful name while declaring variables
  • Also, be consistent. Means, relate the variable name: that you are doing. (eg: sum = x +y; is conventional, sub = x + y; is not.)

Printing the Value of a Variable

So far, in the previous video, we have learned the printf and scanf function and their workflow. printf always display program data to the monitor. And scanf take data from the user keyboard. Here we will use printf function here to display a value of the variable.

Follow the example below to evaluate the rules of variable printing. After the example, we will generalize the rules of printing a value of the variable on the display;

Example Fig1.4:

#include <stdio.h> 

int main() {
    //Initializing the variable while declaration
    int x = 4, y = 8, sum = x + y;
    float a = 5.90, b = 2.30, sub = a - b;
    double t = 130.230, q = 4.95, temp = t/q;
    char ch ='Abc';

    printf("The sum of x & y is: %d\n", sum); // Use %d as container for Integer type
    printf("The sub of a & b is: %0.3f\n", sub); // Use %f as container for floating number
    printf("Temp is: %0.3f\n", temp); // Also use %f for double type
    printf("Character is: %c", ch); // Use %c for charecter type

    return 0;

Output Fig1.4:

C Variables

Using generalized rules above you can print any variable to display.

What is next?

So far we have learned the basic of Variable. In the next tutorial, we will learn the types and scopes of Variable in C.