Signup/Sign In
LAST UPDATED ON: SEPTEMBER 17, 2024

Variables in C (with Examples)

  • A variable is like a container (storage space) with a name in which you can store data.

  • When you create a variable in C programming a new memory space gets assigned to the variable.

  • While creating a new variable you have to provide it a name and specify datatype for the variable.

  • Once you have created the variable, you cannot change its datatype.

C variable example

  • When we create a variable in a C program, the C compiler allocates a storage space, depending upon the data type of the variable(8 bits for char, 16/32 bits for int, etc.)

  • Then that storage space is given a name which is the variable name.

c variable concept

  • Once a variable is created, we can store value in it.

  • We can change the value stored in a variable, but we cannot change the datatype of the value stored in it.

Syntax for using Variable:

Here is how you can create or declare a new variable in the C language,

data_type var_name;

where, data_type is a valid data type (along with datatype modifiers, if required) and var_name is the name of the variable.

For example,

int marks;

Here, marks is the name of the variable, and it can store values of int type.

Assign value to Variable

Once we have declared or created the variable, then we can assign a value to it. This is called variable definition.

// variable declaration
int marks;
// variable definition
marks = 10;

We can do declaration and definition in a single step too, like this (recommended).

int marks = 10;

C Variable declaration and definition

You can change the value of the variable, whenever you want. But make sure the value is of the correct data type.

// variable defined
int marks = 10;
// assign a new value
marks = 33;

Datatype of the Variable

  • A variable in C language must be given a type, which defines what type of data can be stored in the variable.

  • If you do not provide any datatype, then the C compiler will give compile-time error or syntax error.

  • The datatype can be char, int, float, double, or short int, long int, etc. data type along with modifiers.

  • You can learn about C Datatypes to see the list of various data types supported in the C language.

Let's take an example,

// char type variable
char status = 'Y';

// int type variable
int marks = 95;

// float type variable
float percentage = 94.6;

// double type variable
double long = 76.997429;

If you try to assign an incorrect datatype value to a variable, then the compiler may give an error, or it will automatically convert the value into the datatype of the variable.

For example,

#include <stdio.h>

int main() {
   // assign incorrect value
   int x = 10.58;
   printf("Value is %d", x);
   return 0;
}


Value is 10

Run Code →

As you can see in the output, the C compiler removed the part after the decimal, because int datatype can only store whole numbers.

Datatype cannot be changed

Once we define a variable with some datatype then we cannot change the datatype of that variable.

For example,

// int variable
int marks = 10;

float marks;    // error

Rules to Name a Variable

When you create a variable, you should always give a meaningful name to the variable. And follow the below rules for naming the variable:

  1. Variable names must not start with a digit.

  2. The variable name can consist of alphabets, digits, and special symbols like underscore _.

  3. Blank or spaces are not allowed in the variable name.

  4. Keywords are not allowed as a variable name.

  5. Upper and lowercase names are treated as different, as C is case-sensitive, so it is suggested to keep the variable names in lowercase.

Let's see a few examples of incorrect names as per the above rules:

int 1var;    // incorrect - should not start with number
int var1;    // correct

int my$var    // incorrect - special characters not allowed
int my_var1;    // correct

int my var;    // incorrect - spaces not allowed

char else;    // can't use Keywords

int count;    // valid variable name
int Count;    // new variable
int COUNT;    // new variable

Creating a Variable - Behind the Scenes

Declaration of variables must be done before they are used in the program. The declaration does the following things.

  1. It tells the compiler what the variable name is.

  2. It specifies what type of data the variable will hold.

  3. Until the variable is defined the compiler doesn't have to worry about allocating memory space to the variable.

  4. The declaration is more like informing the compiler that there exists a variable with the following datatype which is used in the program.

  5. We can even declare a variable outside the main() function, using the extern keyword.

extern int a;
extern float b;
extern double c, d;

Defining or Initializing a variable means the compiler has to now assign storage to the variable because it will be used in the program.

We can even declare multiple variables of the same data type in a single line by using a comma to separate them.

For example,

int a;
float b, c;

Initializing a variable means providing it with a value.

int a = 10;

 

Difference between a Variable and an Identifier?

An Identifier is a name given to any variable, function, structure, pointer, or any other entity in a programming language. While a variable, as we have just learned in this tutorial is a named memory location to store data that is used in the program.

Identifier Variable
The identifier is the name given to a variable, function, etc. While a variable is used to name a memory location that stores data.
An identifier can be a variable, but not all identifiers are variables. All variable names are identifiers.
Example:
// studytonight is identifier for a variable
int studytonight;

// studytonight is identifier for a function
int studytonight() { 
    ... 
}
Example:
// int variable
int a;
// float variable
float a;