Variables Constant and Literals In C

Variables Constant and Literals In C

You will learn about Variables Constant Literals and the conventions for the variable names in this course. Additionally, you will discover the various C programming literals available and how to create constants.


A variable is a storage place (container) for data in programming.

Simply said, a variable is a storage location with RAM allotted to it. A variable is essentially used to store some kind of data.

Variables of different types require varying amounts of memory, various sorts of memory locations, and a certain set of operations that can be used on them.

An upper- and lower-case alphabet, digits, and the underscore character (‘_’) are all acceptable components of variable names. The name cannot, however, begin with a number.

Each variable needs to have a distinct name to designate the storage space (identifier). The only thing that variable names are is a memory location’s symbolic representation. For instance:

int playerScore = 95;

PlayerScore is an int-type variable in this situation. In this instance, the variable is given the integer value 95.

Variables can have their values modified, hence the term.

char ch = 'a';
// some code
ch = 'l';

Guidelines for variable naming(Variables Constant and Literals)

Only letters (including uppercase and lowercase characters), numbers, and underscores are permitted in variable names.

A variable’s first letter must either be a letter or an underscore.

There is no restriction on the length of a variable name or identifier. However, if the variable name is larger than 31 characters, you can have issues in some compilers.

Always make an effort to give variables names that make sense. As an illustration, firstName is a superior variable name to fn.

Languages in C are heavily typed. This indicates that once a variable is declared, its type cannot be altered. For instance:

int number = 5;      // integer variable
number = 5.5;        // error
double number;       // error

The number variable’s type in this instance is int. This variable cannot be given the floating-point (decimal) value of 5.5. Additionally, you cannot change the variable’s data type to double. By the way, you must define the type of the decimal numbers in C as either double or float.

Literals(Variables Constant and Literals)

Constants are fixed values that cannot be changed by the program while it is running. Literals are another name for these fixed values.

Any of the fundamental data types, such as an integer constant, a floating-point constant, a character constant, or a string literal, can be used as constants. Enumeration constants are also present.

The only difference between constants and ordinary variables is that once a constant’s value has been defined, it cannot be changed.

Literals are information that is used to represent fixed values. They can be inserted right into the code. Examples include: 1, 2.5, “c,” etc.

Literals in this case are 1, 2.5, and ‘c’. Why? These terms cannot have a range of values.

Integers(Variables Constant and Literals)

A numeric literal (one connected with numbers) without a fractional or exponential component is called an integer. In C programming, there are three different kinds of integer literals:

  • decimal (base 10)
  • octal (base 8)
  • hexadecimal (base 16)

For instance:

Decimal: 0, -9, 22 etc
Octal: 021, 077, 033 etc
Hexadecimal: 0x7f, 0x2a, 0x521 etc

Hexadecimal and octal both begin with a 0x in C programming.

Literals with floating points

A number literal that has either a fractional form or an exponent form is referred to as a floating-point literal. For instance:


Note: E-5 = 10-5


A single character can be turned into a literal by surrounding it in single quotation marks. For instance, “a,” “m,” “F,” “2,” “,” etc.

Escape Sequences(Variables Constant and Literals)

In C programming, it may occasionally be required to employ characters that cannot be typed or have unique meanings. As an illustration, use a newline (enter), tab, question mark, etc.

Escape sequences are used to access these characters.

Escape SequencesCharacter
\fForm feed
\tHorizontal tab
\vVertical tab
\’Single quotation mark
\”Double quotation mark
\?Question mark
\0Null character

For instance, a newline is denoted by n. The backslash (/) causes escape from the compiler’s default handling of the characters.

String Literals(Variables Constant and Literals)

A series of characters contained in double-quote marks is referred to as a string literal. For instance:

“good” //string constant
“” //null string constant
” ” //string constant of six white space
“x” //string constant having a single character.
“Earth is round\n” //prints string with a newline

Constants(Variables Constant and Literals)

A constant is a value that the program cannot change while it is normally running, i.e., the value is constant.

Although the phrases “constant” and “named constant” are frequently used interchangeably, a constant is considered to be “named” when it is coupled with an identifier.

Unlike a variable, which is an identifier with a variable value that might vary during normal execution, a variable is not fixed. [1]

The const keyword can be used to define a variable whose value cannot be modified. A constant will result from this. For instance,

const double PI = 3.14;

Notice, that we have added keyword const.

Here, PI is a symbolic constant; its value cannot be changed.

const double PI = 3.14;
PI = 2.9; //Error

You can also define a constant using the #define preprocessor directive. We will learn about it in the C Macros tutorial

Leave a Reply