C C++

C Variables


Variables in C are identifiers assigned to memory locations, allowing us to reference them without needing to remember their memory addresses.

Defining a variable consists of three main aspects:

  • Variable Declaration
  • Variable Definition
  • Variable Initialization

Definition and Initialization in C

Syntax

// defining single variable
data_type variable_name; 

 // defining multiple variable
data_type variable_name_1, variable_name_2;   

// defining single variable
data_type variable_name = value; 

 // defining multiple variable
data_type variable_name_1 = value_1, variable_name_2 = value_2;   

Example

Run Code
#include 

int main() {
    // Defining a single variable (without initialization)
    int a; 

    // Defining multiple variables (without initialization)
    int b, c;

    // Defining a single variable (with initialization)
    int d = 5;

    // Defining multiple variables (with initialization)
    int e = 10, f = 20;

    // Assign values to the uninitialized variables
    a = 3;
    b = 7;
    c = 9;

    // Output the values of the variables
    printf("Single variable (a): %d\n", a);
    printf("Multiple variables (b, c): %d, %d\n", b, c);
    printf("Single variable with value (d): %d\n", d);
    printf("Multiple variables with values (e, f): %d, %d\n", e, f);

    return 0;
}