loader image

Variables and Constants in C

In this tutorial, you will learn about variables and constants in C.


A variable is a data name that can be used to store data. A variable may take different values at different times during the execution of a program. A variable name can be meaningfully chosen by the programmer, subject to the conditions listed below:

  • Variable names can be made up of alphabets, digits, and the underscore character.
  • They must begin with an alphabet. Some systems allow underscore as the initial character.
  • Uppercase and lowercase letters are significant. That is, the variable Mark is not the same as mark or MARK.
  • It should not be a keyword.
  • White space is not allowed.

Some examples of valid variable names include Value, score, mark1, phone_number, Counter_1, etc.

Invalid examples include 123, (area), 22nd , char, etc.

Declaration of Variables

Declaration tells the compiler what the variable name is and what type of data the variable will hold. In C, variables can be declared using the following syntax:

datatype variable1;

Multiple variables can be declared by separating them with commas as follows.

datatype variable1, variable2, variable3;

For example,

int x;
float a, b, c;

Here int and float are the keywords used to represent integer and real numbers respectively.

Learn more about data types in this tutorial.

Initializing variables

Assigning value to a variable once declared is called initialization of a variable. In C, the varibales declaration and initialization together takes the following form:

int x = 5;

The following ways are also acceptable.

int a, b=10, c = 5;
int x, y, z;
x = 10;
y = z = 5;

Here b = 10, c = 5, x =10, y= 5, z = 5.


Fixed values that do not change throughout program execution are called constants. The keyword const can be used to define a constant as follows.

const datatype name = value;

For example const double pi = 3.14; will define a constant named ‘pi’ of type ‘double’ having the value ‘3.14’. The value of pi can not be changed throughout the program.

Constants can also be defined using the #define preprocessor directive. You can learn about the symbolic constants in this tutorial.

C supports the following types of constants.

c constants
Types of constants in C

Integer Constants

A sequence of digits is referred to as an integer constant. Spaces, commas, and non-digit characters are not permitted between digits. The integer constants are of three types.

  • Decimal integer – Consist of a series of digits from 0 to 9, followed by an optional – or + symbol.
  • Octal integer – Made up of any combination of digits from 0 to 7, with a leading 0.
  • Hexadecimal integer – Series of digits preceded by 0x or 0X.

Decimal Integer: 120 -450 1 654321 +56
Octal Integer: 031 0 0321 04320
Hexadecimal: 0x2F 0X1A2 0xD10 0x

Real Constants

Numbers containing fractional parts are called real constants. Examples of real constants include 452.5 -.74 +321.2 etc.

Single Character Constants

A single character enclosed within a pair of single quote marks is called a single character constant. Examples of single character constants include 'F' 'c' '3'.

String Constants

A sequence of characters enclosed in double quotes is called a string constant. Letters, numerals, special characters, and blank spaces can all be used as characters. Some examples are "Hello" "Good Morning" "1996" "what?" etc.

Backslash Character Constants

C includes various special backslash character constants that can be used in output functions. They are known as escape sequences. The following table shows the list of backslash character constants in C.

Constant Meaning
\a Bell
\b Back space
\f Form feed
\n New line
\r Carriage return
\t Horizontal tab
\v Vertical tab
\’ Single quote
\” Double quote
\? Question mark
\\ Back slash
\0 Null
Notify of
Inline Feedbacks
View all comments