Variables in C Programming

Variables

Variables are placeholders in memory used by a program to store data. The size of this memory block depends on the range over which the variable is allowed to vary.

For example, on a personal computer, the size of an integer variable is typically two bytes, and that of a long integer is four bytes.

A variable serves to temporarily hold a number or string value as required by the program. To identify variables, each is given a unique name, referred to as a variable name. Before using a variable, it is necessary to declare it, which involves specifying the variable's name and data type that it can store.

The format for declaring a variable in C is as follows:

[Storage-class] type variable_name [= initial_value];

Both the storage class and the initial value can be omitted if not required.

The same data type and storage class variable can be declared, separated by commas.

[Storage-class] type  variable_name [= initial value], variable [= initial value], variable [= initial value];

In C the size of a variable type such as an integer need not be the same on all types of machines. When we declare a variable we inform the compiler of two things, the name of the variable and the type of the variable. For example, we declare a variable of type character with the name i by writing:

        char i;

On seeing the "char" part of this statement the compiler sets aside one bytes of memory to hold the value of the character. It also sets up a symbol table. In that table it adds the symbol i and the relative address in memory where those one byte was set aside. Thus, later if we write: i = 'x'; we expect that,at run time when this statement is executed, the value 'x' will be placed in that memory location reserved for the storage of the value of i.

Following are the rules for naming the variables:

  • All variables must be declared before they can appear in executable statement.

  • A declaration consists of a data type followed by one or more variable names separated by commas.

    Example:

     int a,b,c;
    
  • Variables can be distributed among declarations in any fashion. The above declaration can be written as

     int a;
     int b,c;
    
  • Integer type variables can be declared to be short integer for smaller integer quantities or long integer for larger integer quantities.

    Example:

    short int a,b,c;
    long int a,b,c;
    
  • An integer variable can also be declared to be un signed by writing unsigned int.

    Example:

    unsigned int;
    
  • Example:

Ready to get started?

Ready to embark on your journey into the world of C programming? Our comprehensive course provides the perfect starting point for learners of all levels. With engaging lessons, hands-on exercises, and expert guidance, you'll gain the skills and confidence needed to excel in this fundamental programming language. Let's dive in and unlock the endless possibilities of C programming together!