Table of Contents

    Variable Declaration and Initialization in C: A Comprehensive Guide

    Variable Declaration and Initialization in C: A Comprehensive Guide

    C variables are names used for storing a data value to locations in memory. The value stored in the c variables may be changed during program execution.

    C is a strongly typed language. Every variable must be declared, indicating its data type before it can be used. The declaration can also involve explicit initialization, giving the variable a value; a variable that is declared but not explicitly initialized is of uncertain value (and should be regarded as dangerous until it is initialized). In K&R C, declarations must precede all other statements, but in modern versions of C, this rule is relaxed so that you don't have to declare a variable until just before you start using it:

    The basic form of a variable declaration is shown here:

    type identifier [ = value][, identifier [= value] ...] ;
    • The type is one of C's data types like int, chat, double etc.
    • The identifier is the name of the variable. You can initialize the variable by specifying an equal sign and a value. Keep in mind
    • You can initialize the variable by specifying an equal sign and a value. Keep in mind that the initialization expression must result in a value of the same (or compatible) type as that specified for the variable.
    • To declare more than one variable of the specified type, use a comma-separated list.

    Declaration of Variable

    Declaration of variable in c can be done using following syntax:

    data_type variable_name;ordata_type variable1, variable2,…,variablen;

    where data_type is any valid c data type and variable_name is any valid identifier.

    For example,

    int a;
    float variable;
    float a, b;

    Initialization of Variable

    C variables declared can be initialized with the help of assignment operator ‘=’.

    Syntax

    data_type variable_name=constant/literal/expression;orvariable_name=constant/literal/expression;

    Example:

    int a=10;
     int a=b+c;
     a=10;
     a=b+c;

    Multiple variables can be initialized in a single statement by single value, for example, a=b=c=d=e=10;

    NOTE: C variables must be declared before they are used in the c program. Also, since c is a case-sensitive programming language, therefore the c variables, abc, Abc and ABC are all different.

    int Variable Declaration and Variable Initialization in two steps:

    Save Source File Name as : IntExample.c Program
    
    /*
      C program to demonstrate Declaration and initialization
      C program to demonstrate int data type
    */
    
    #include<stdio.h>
    void main()
    {
       int age;  // Declaration of variable age
       age = 20;  // initialization of the variable age
       printf("%d \n",age);
    }
    
    Output
    20
    Press any key to continue . . .
    Variable Declaration and Initialization

    int Variable Declaration and Variable Initialization in one line:

    Variable Declaration and Initialization
    
    /*
      C program to demonstrate Declaration and initialization
      C program to demonstrate int data type
    */
    
    #include<stdio.h>
    void main()
    {
       int age=20;  // Declaration and initialization of variable age     
       printf("%d \n",age);
    }
    
    Output
    20
    Press any key to continue . . .

    Multiple variables can be initialized in a single statement by single value

    a = b = c = d = e = 10;
    
    /*
      C program to demonstrate Declaration and initialization
      C program to demonstrate int data type
    */
    
    #include<stdio.h>
    void main()
    {
          
       int a, b, c, d, e; // Declaration of variable  
        a=b=c=d=e=10; // initialization of variable  
       printf("%d \n",a);
       printf("%d \n",b);
       printf("%d \n",c);
       printf("%d \n",d);
       printf("%d \n",e);
    }
    
    Output
    10
    10
    10
    10
    10
    Press any key to continue . . .

    Point To remember

    • Variables should be declared in the C program before to use.
    • Memory space is not allocated for a variable while declaration. It happens only on the variable definition.
    • Variable initialization means assigning a value to the variable.
     Type 
    Syntax
    Variable declaration
    data_type variable_name;
    Example: int x, y, z; char flat, ch;
    Variable initialization
    data_type variable_name = value;
    Example: int x = 50, y = 30; char flag = ‘x’, ch=’l’;