January 25, 2023

Pay Dayz Blog

Blogging and News Press

Variables in C- Check GATE Notes For CSE

3 min read
Gate notes

The term Variables in C refer to the names of the memory locations. We use variables to store data. The variable values can be altered and then reused multiple times according to our convenience. In simple words, variables are a way to represent memory location through symbols so that they can be identified easily by users.

In this article, we will discuss more on variables in C. Are you a GATE aspirant? Read ahead to know more and check GATE Notes For CSE here to discover more topics.

Definition of Variables

Here’s the syntax to declare a given variable:

type variable_list; 

Example of declaring the variable:

int x;

float y;

char z;

Here, x, y, z are variables, and the int, float, char are the data types. One can also provide values while declaring the variables like this:

int x=10, y=20; //declaration of 2 variable of int data type

float c=20.8;

char z=’X’;

Naming Variables

Rules for defining variables:

  • Variables can have digits, alphabets, and underscore.
  • Variable names can begin with the alphabets and underscore only.
  • The variable name can NOT begin with a digit.
  • There must be no whitespace within the variable name.
  • The variable names must not be any reserved keyword or word, ex., float, int, etc.

Examples of valid variable names:

int b;

int _bf;

int b70;

Examples of invalid variable names:

int 5;

int b f;

int long;

Types of Variables in C

Variables can be of the following types in a C program:

  1. global variable
  2. local variable
  3. automatic variable
  4. static variable
  5. external variable

Global Variable

A global variable refers to the one that is declared outside the function or the block. Any function in a program is capable of changing the value of the global variable. Meaning, this variable is available to all the functions. Keep in mind that it must be declared at the very start of the block.

Example:

int value=50; // A Global variable

void function1(){

int a=20; // A Local variable

}

Local Variable

A local variable refers to the variable that is declared inside the given function or the block. We always declare a local variable at the start of the block. Also, we have to initialize it before using it in a program.

Example:

void function1(){

int a=20; // A Local variable

}

Automatic Variable

Any variable in the C language that is declared inside the block is an automatic variable by default. We can declare an automatic variable explicitly using the auto keyword.

void main(){

int a=10; // A Local variable (also automatic)

auto int b=20; // An Automatic variable

}

Static Variable

A static variable refers to that variable that we declare using the static keyword. The static variable retains its value between multiple function calls.

Example:

void function1(){

int a=20; // A Local variable

static int b=20; // A Static variable

a=a+1;

b=b+1;

printf(“%d,%d”,a,b);

}

If we call this function multiple times, the local variable will print the very same value for every function call, ex., 21, 21, 21 and so on. On the other hand, the static variable would actually print the incremented value in every function call, ex. 21, 22, 23 and so on.

External Variable

We use the external variable to share the variable in multiple C source files. We need the extern keyword to declare any external variable.

Example:

extern int a=20;// An external variable (also global variable) 

#include “myfile.h”

#include <stdio.h>

void printValue(){

    printf(“The global variable is: %d”, global_variable);

}

We hope this article was helpful. If you are a GATE aspirant, then you might want to learn more about a similar concept. Check the full details of Variables in C here. Develop a timetable that works best for you and prepare for your GATE exams with maximum confidence. All the best!