Variables in C

Variables in C
Techiio-author
Written by Shuvhojit DebJanuary 31, 2022
12 min read
C
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Techiio-author
Shuvhojit Deb

Full Stack Developer

Today we'll know about the variables of C.

Definition of Variables in C

A variable definition tells the compiler where and how much storage to create for the variable. A variable definition specifies a data type and contains a list of one or more variables of that type as follows −

type variable_list;

Here, type must be a valid C data type including char, w_char, int, float, double, bool, or any user-defined object; and variable_list may consist of one or more identifier names separated by commas. Some valid declarations are shown here −

int    i, j, k;
char   c, ch;
float  f, salary;
double d;

The line int i, j, k; declares and defines the variables i, j, and k; which instructs the compiler to create variables named i, j, and k of type int.

Variables can be initialized (assigned an initial value) in their declaration. The initializer consists of an equal sign followed by a constant expression as follows −

type variable_name = value;

Some examples are −

extern int d = 3, f = 5;    // declaration of d and f. 
int d = 3, f = 5;           // definition and initializing d and f. 
byte z = 22;                // definition and initializes z. 
char x = 'x';               // the variable x has the value 'x'.

For definition without an initializer: variables with static storage duration are implicitly initialized with NULL (all bytes have the value 0); the initial value of all other variables are undefined.

How to Work?

  • While declaring variables, it tells compilers the type of data it holds.
  • Variables tell compilers the name of the variables that are being used in the program.
  • As variables specify storage, compilers do not have to worry about the variables’ memory location until they are declared.

Declaration of Variables in C

A variable declaration assures the compiler that there exists a variable with the given type and name so that the compiler can proceed with further compilation without requiring the complete detail about the variable. A variable definition has its meaning at the time of compilation only, the compiler needs actual variable definition at the time of linking the program.

A variable declaration is useful when you are using multiple files and you define your variable in one of the files which will be available at the time of linking the program. You will use the keyword extern to declare a variable at any place. Though you can declare variables multiple times in your C program, they can be defined only once in a file, a function, or a block of code.

Example

Try the following example, where variables have been declared at the top, but they have been defined and initialized inside the main function −

#include <stdio.h>
// Variable declaration:
extern int a, b;
extern int c;
extern float f;
int main () {
   /* variable definition: */
   int a, b;
   int c;
   float f;
 
   /* actual initialization */
   a = 10;
   b = 20;
  
   c = a + b;
   printf("value of c : %d \n", c);
   f = 70.0/3.0;
   printf("value of f : %f \n", f);
 
   return 0;
}

When the above code is compiled and executed, it produces the following result −

value of c : 30
value of f : 23.333334

The same concept applies to function declaration where you provide a function name at the time of its declaration and its actual definition can be given anywhere else. For example −

// function declaration
int func();
int main() {
   // function call
   int i = func();
}
// function definition
int func() {
   return 0;
}

How to Initialize?

Initializing variables in C means allocating values to variables directly while declaring it. The syntax for initializing variables are as follows:

data_type variable_name = value;

Example

  1. int a = 10;
  2. int a = 5, b = 8;

In example 1, variable a is created and initialized with the value 10. For example, 2 two variables, a and b, are created allocated values 5 and 8, respectively.

Program to illustrate initialization of variables in C.

#include<stdio.h>
#include<conio.h>
int main()
{
int m = 2, n = 3;
z = m + n;
printf("Sum of two numbers is: %d \n", z);
return 0;
}

Types of Variables in C

There are many types of variables in c:

  • Local variable
  • Global variable
  • Static variable
  • Automatic variable
  • External variable

Local Variable

A variable that is declared inside the function or block is called a local variable.

It must be declared at the start of the block.

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

You must have to initialize the local variable before it is used.

Global Variable

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

It must be declared at the start of the block.

int value=20;//global variable  
void function1(){  
int x=10;//local variable  
}  

Static Variable

A variable that is declared with the static keyword is called a static variable.

It retains its 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 you call this function many times, the local variable will print the same value for each function call, e.g, 11,11,11 and so on. But the static variable will print the incremented value in each function call, e.g. 11, 12, 13, and so on.

Automatic Variable

All variables in C that are declared inside the block, are automatic variables by default. We can explicitly declare an automatic variable using an auto keyword.

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

External Variable

We can share a variable in multiple C source files by using an external variable. To declare an external variable, you need to use the extern keyword.

myfile.h

extern int x=10;//external variable (also global)  

program1.c

#include "myfile.h"  
#include <stdio.h>  
void printValue(){  
    printf("Global variable: %d", global_variable);  
}
C Language
C Variables
Programming
Software
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance