Home / Computer Science / C Language / Variables in C language

Variables in C language

A variable is name which can hold different values at different instances. Here in C a variable is name of memory location used to store data or value. Its value can be changed and it can be reused many times. It is one of the way to represent memory location through symbol or character so that it can be easily identified. Each variable in C has a specific type, which determines the size and layout of the variable’s memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.

 

A variable may be a letter, character or combination of characters and digits with special symbol underscore(_).

We know c is case sensitive so there always difference between A and a.

 

Syntax for variable declaration:

Data type variable_list;

For example

int  a;

float b;

char c;

double d;

See the below variable types in C:

  1. Local variable
  2. Global variable
  3. Static variable
  4. Automatic variable
  5. External variable

Local Variable:

A variable which is declared within the block pr function is called local variable. Its scope is limited to that function or block or within the block.

A local variable must be declared starting of the block/function.

Void function_name(){

Int abc=90; // local variable declared starting of the block/function.

…..stmts;

}

Global Variable: A variable is declared outside of the function is called is global variable. Its scope is throughout the program and it is available to alla other functions in the program.

Float xyz=0.999;//global variable declared starting of the block

Void function_name(){

Int abc=90; // local variable declared starting of the block/function.

…..stmts;

}

Stmts;

 

Static variable:  A static variable is declared by using static key word and the variable is called static variable.

Retains its value between multiple function calls.
Static is used in the C programming language with global variables and functions to set their scope to the containing file. In local variables, static is used to store the variable in the statically allocated memory instead of the automatically allocated memory.

static data_type var_name = var_value;

#include<stdio.h>

int fun()

{

static int count = 0;

count++;

return count;

}

 

int main()

{

printf(“%d “, fun());

printf(“%d “, fun());

return 0;

}

Output is:  1 2

Note : If the static is not used here then the output is 1 1. Static variable will print the incremented value.

Automatic Variable: By using auto keyword we declare automatic variables. In c all variables is declared inside the block, are automatic variables by default.

int main(){

int x =19;

auto int y=199; // automatic variable.

}

External variable:  eextern is used to let other C files or external components know this variable is already defined somewhere. We can share a variable in multiple C source files by using external variable.

Crate a file named with extendemo.h.

In externdemo.h

extern int xyz=1009;//external variable

create a c program to use this file.

#include “externdemo.h”

#include<stdio.h>

void demoExtern(){

printf(“Global variable: %d”,xyz”);

}

It prints:  Global Variable 1009.

 

About GSK

Hi, i am Santosh Gadagamma, a tutor in Software Engineering and an enthusiast for sharing knowledge in Computer Science and other domains. I developed this site to share knowledge to all the aspirants of technologies like, Java, C/C++, DBMS/RDBMS, Bootstrap, Big Data, Javascript, Android, Spring, Hibernate, Struts and all levels of software project design, development, deployment, and maintenance. As a programmer I believe that, "The world now needs computers to function." Hope, this site guides you as a learning tool towards greater heights. I believe that Education has no end points and i wish to learn more in the process of teaching you.

Check Also

Features of C language

WHERE is C Used Let us now see how does C compare with other programming …