C Variables

In this tutorial, you will learn about C variables and rules for naming a variable.


A variable is nothing but a name provided to a storage location that can store data. It is the way to represent memory location using symbols so that it can be easily identified. The value of the variable can be changed and reuse many times.

The name of the variable should be unique (identifier) across the program in order to easily identify.

C variable syntax

Let’s check the syntax of a variable in C.

type variable_name;

C variable example

The following is the example of declaring variables in C.

int a; 
float b; 
char c;

Here, a, b, c are the variable name and int, float and char are the data types for these variables.

We can also provide values while declaring a variable as follows:

int a=5,b=10;
float x=2.5;
char c='A';

Rules for naming a variable

  1. A valid variable name can only have alphabets (both uppercase and lowercase), underscore, and digits.
  2. The first character of a variable name should be either an alphabet or underscore. It can’t start with a digit.
  3. No white space is allowed in a variable.
  4. A variable name must not be any reserve keywords like int, float, etc.
  5. There is no rule on how long a variable name (identifiers) should be. However, you may get errors in some compiler if the length of the Identifiers is more than 31 characters.
  6. The name of the variable should be written in such a way that is meaningful, short, and easy to read.

Valid variable names

Following are some of the examples of valid variable names.

int a; 
int _ab; 
int a50;

Invalid variable names

Following are some of the examples of invalid variable names.

int 5; 
int a b; 
int auto;

Types of Variables in C

There are many types of variables available in the C language.

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

1) Local variable

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

It must be declared at the start of the block. The scope of the variable is inside that function or block only.

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

The variable must be initialized before its use.

2) Global variable

A variable that is declared outside the function or block is called a global variable. The scope of the global variable is global which means any function can access and change the value of a global variable.

It should be declaring starting of any functions.

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

3) Static variable

A variable that is declared using a static keyword is called a static variable.

The main difference between the normal variable and static variable is that it retains its value between multiple function calls.

For example:

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

If you execute the above many times,  the local variable x will print the same value i.e. 20,20,20, and so on in each function call. On the other hand, the static variable y will retain its value after each function call and the output will be like 20, 21, 22, and so on.

4) Automatic variable

All the variables those are declared inside any function or block is automatic variable by default. You can explicitly mention the type of the automatic variable using the auto keyword.

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

5) External variable

A variable that can be shared between multiple C programs is called an external variable.

To declare an external variable, you need to use the extern keyword during declaration.

myfile1.h

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

program1.c

#include "myfile1.h" 
#include <stdio.h> 
void main(){ 
printf("Global variable: %d", global_variable); 
}

 

Please get connected & share!

Advertisement