Detailed Explanation of Variables in C Language

A variable is a name for a memory location. It is used to store data. The value of a variable can change and can be reused multiple times.

Memory locations are represented by symbols so they can be easily identified.

Let’s look at the syntax for declaring a variable:

type variable_list;  

An example of declaring variables is as follows:

int a;float b;char c;

Here, a, b, and c are variables. int, float, and char are data types.

We can also provide values when declaring variables, as shown below:

int a = 10, b = 20; // Declare two integer type variablesfloat f = 20.8;char c = 'A';

Rules for Defining Variables

  • Variables can consist of letters, numbers, and underscores.

  • Variable names can start with a letter or an underscore but cannot start with a number.

  • Variable names cannot contain spaces.

  • Variable names cannot be reserved words or keywords, such as int, float, etc.

Valid Variable Names
int a;int _ab;int a30;

Invalid Variable Names

int 2;int a b;int long;

👇 Click to Claim👇

👉 Collection of C Language Knowledge Materials

Variable Types in C

There are many types of variables in C language:

  • Local Variables

  • Global Variables

  • Static Variables

  • Automatic Variables

  • External Variables

Local Variables

Variables declared within a function or block are called local variables.

They must be declared at the beginning of the block.

void function1(){   int x = 10; // Local variable}

Local variables must be initialized before use.

Global Variables

Variables declared outside of a function or block are called global variables. Any function can change the value of a global variable. It is available to all functions.

It must be declared at the beginning of the block.

int value = 20; // Global variablevoid function1(){   int x = 10; // Local variable}

Static Variables

Variables declared with the static keyword are called static variables.

They retain their value between multiple function calls.

void function1(){   int x = 10; // Local variable   static int y = 10; // Static variable   x = x + 1;   y = y + 1;   printf("%d, %d", x, y);}

If the function is called multiple times, the local variable will print the same value for each function call, such as 11, 11, 11, etc. However, the static variable will print increasing values for each function call, such as 11, 12, 13, etc.

Automatic Variables

In C language, by default, all variables declared within a block are automatic variables. We can explicitly declare automatic variables using the auto keyword.

void main(){    int x = 10; // Local variable (also an automatic variable)    auto int y = 20; // Automatic variable}

External Variables

We can use external variables to share a variable across multiple C source files. To declare an external variable, we need to use the extern keyword.

myfile.h

extern int x; // External variable (also a global variable)

program1.c

#include "myfile.h"#include <stdio.h>
void printValue(){    printf("Global variable: %d", x);}

Note: When defining an external variable in one file and using it in another file, ensure that it is initialized in the defining file before use.

Programmer Technical Exchange Group

Scan the code to join the group, remember to note: city, nickname, and technical direction.




Leave a Comment