fbpx

The Fundamentals of Variables in C Programming

The Fundamentals of Variables in C Programming

INTRODUCTION

In C programming, variables are the building blocks of data storage, allowing you to assign, modify, and retrieve data as your program executes. Simply put, a variable is a reserved space in memory that stores a value with a specific data type. Unlike constants, a variable’s value is not fixed; you can change it multiple times throughout the program, making it highly versatile.

When defining variables in C, understanding naming rules, syntax, and memory allocation is essential. Each variable has three primary stages: declaration, definition, and initialization. Missing any of these can lead to unwanted results, such as random or “junk” values if the variable is left uninitialized.

VARIABLES:

  • The term used to describe the various types of variables in a memory location.
  • Its value can be changed, and it can be reused many times.
  • A variable is defined as the reserved memory space which stores a value of definite datatypes.
  • The value of variable is not constant, instead it allow changes.
  • We can assign name; value for the variables, but the “address” is assigned by the complier.
  • Once variables is declared it must initialize, if not the value will be junk/garbage/random value.

Rules for writing the variables:

It can starts with underscore.

It can starts with alphabets.

It should start with numbers.

There should not be any whitespaces.

Syntax of variables:

data_type variable_name = value;   (defining single var)

            or

data_type variable_name1, variable_name2;  (defining multiple var)

valid variable names:

int a;

char _c;

float  f_data;

invalid variable names:

int auto

int !a

int 2hello

char @e;

Three stages of variable:

  1. Variable Declaration
  2. Variable Definition
  3. Variable Initialization

1. Variable Declaration:

It informs the compiler about the existence of variables and their respective data types.

data_type variable_name ;   (defining single var)

Eg : int a;

 char  c

float f

2. Variable Definition:

data_type variable_name = value;    (defining single var)

int a=10;

char c=’a’;

float f=12.22f;

3. Variable Initialization:

data_type variable_name = value;   ( defining single var)

int num=10;

Types of variables

  1. Local Variables
  2. Global Variables
  3. Static Variables
  4. Automatic Variables
  5. Extern Variables
  6. Register Variables

1. Local variables:

  • Definition:defined within a function or block
  • This is only be accessed within that specific block.
  • Storage Class: By default, local variables are stored in the stack.

#include

void myFunction() {

    int a = 10;

    printf(“%d”, a);

}

2. Global Variables

  • Definition: A global variable is declared outside of all functions, usually at the top of the program.
  • Lifetime: The lifetime of a global variable is the entire runtime of the program, meaning it is created when the program starts and destroyed when the program ends.

Example:

 #include  int a = 10;  // Global variable void myFunction() {    printf(“%d”, a);  // Accessing global variable} int main() {    myFunction();    return 0;}   

3. Static Variables:

  • Definition: A static variable retains its value across multiple function calls, initializing only once and persisting even after the function exits.
  • Scope: When declared within a function, a static variable is local to that function but keeps its value between calls. If declared outside any function, its scope is limited to the file.
  • Storage Location: Static variables are stored in the data segment.
  • Storage Location: Static variables are stored in the data segment.

Without Static:—————

#include

void myFunction() {

     int count = 0;  //( Static var)

    count++;

    printf(“%d\n”, count);

}

int main() {

    myFunction();  // Output: 1

    myFunction();  // Output: 2

    myFunction();  // Output: 3

    return 0;

}

Output: 1,1,1;

With static

————-

#include

void myFunction() {

     static int count = 0; 

    count++;

    printf(“%d\n”, count);

}

int main() {

    myFunction();  // Output: 1

    myFunction();  // Output: 2

    myFunction();  // Output: 3

    return 0;

}

Output :1,2, 3

4. Automatic variable:

  • Definitiion: the default type of local variable in C.
  • Scope: The local to the function or block
  • Storage Location: Automatic variables are stored in the stack.

Note: In C, automatic variables are the default type for local variables unless specified otherwise (e.g., with static or register).

Example:

 #include  void myFunction() {    int x = 10; (Automatic variable)    printf(“%d\n”, x);} int main() {    myFunction();  // Output: 10    return 0;}

5. Extern variables:

When you declare a variable with extern, it doesn’t allocate space for the variable;

it merely references a variable that is declared elsewhere (usually in another file or outside the current scope). This allows multiple files to share the same variable, which is often useful in large projects.

#include

int globalVar = 100;  // Define global variable

int main() {

    printf(“Global variable in file1.c: %d\n”, globalVar);

    return 0;

}

#include

extern int globalVar = 100;  // Define global variable

int main() {

    printf(“Global variable in file2.c: %d\n”, globalVar);

    return 0;

}                      

6. Register keyword:

In C programming, the register keyword is used to suggest that a variable should be stored in a CPU register rather than in regular memory (RAM).

This is because CPU registers are faster to access than memory, and using registers for frequently accessed variables can improve the program’s performance.

 However, modern compilers often optimize this automatically, and the register keyword may be ignored.

#include

int main() {

    register int i;  // declare register variable

    for (i = 5; i < 50; i++) {

        printf(“%d “, i);

    }

    return 0;

}