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
Invalid examples include
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:
Multiple variables can be declared by separating them with commas as follows.
datatype variable1, variable2, variable3;
int x; float a, b, c;
float are the keywords used to represent integer and real numbers respectively.
Learn more about data types in this tutorial.
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;
b = 10,
c = 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;
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.
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.
Numbers containing fractional parts are called real constants. Examples of real constants include
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
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
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.