Signup/Sign In

C Variables (with Examples)

In this tutorial, we will be learning what are Variables, how to declare and define a variable in the C language, and what are constants and literals in the C language.

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

C variable example

It's like your home has an address (House No./Flat No.) so whenever you order something online, you just have to provide your home address and the delivery executive is able to find it.

In the C language, if we want to use some data value in our program, how can we do it? Well, it's done using variable.

When we create a variable in a C program, the C compiler allocates a storage space, depending upon the datatype of the variable(8 bits for char, 16/32 bits for int, etc.), and 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 as many times as we want, but we should make sure we store the correct data type value.

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.

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 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 giver 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.

We will learn about printf() function and %d (used to print the value of the int datatype) in the next tutorial, with more code examples.

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 name 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 lower case names are treated as different, as C is case-sensitive, so it is suggested to keep the variable names in lower case.

Let's see a few examples for 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 Variable and 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 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;

Another great analogy to understand the difference between Identifier and Variable is:

You can think of an Identifier int x to be a variable's name, but it can also be a function's name int x() { } and still, be an identifier.

Just like Obama is the name of a person, but also the name of a foundation.

 

Conclusion

In this tutorial we learned about C Variables, how to create a variable, assign value to a variable and what happens when a variable is created. We also learned about the importance of datatype while creating a new variable.



Want to learn coding and don't know where to start?

Try out our Interactive Courses for Free 🥳 😯 🤩
learn to code footer Ad