If we want to use some data value in a C program, we can also directly use a value, instead of creating a variable to store the value. When we use a value directly in C program without creating a variable, it is known as a Literal.
A literal is used when we want to use a fixed value in the program, hence literals are also called Constants.
For example, 1, 100, 'Y', 10.5, etc. Whereas, when we use a variable in C, we can change the value stored in the variable.
In the language, literals can be of 5 types, they are:
Backslash Character Literals (Escape Sequences)
Any numeric value without any decimal or exponential part, used in the C program is an integer literal.
Integer literals are of 3 types:
Decimal Numer (base 10)
Octal Number (base 8) - Uses digits 0 up to 7. Number 10 is the same as 8 for the octal system.
Hexadecimal Number (base 16) - Uses digits 0 up to 9, and alphabets A to F. (A, B, C, D, E, F stands for 10, 11, 12, 13, 14, and 15)
Decimal: 7, -10 etc Octal: 023, 045 etc Hexadecimal: 0x2a, 0x521 etc
In the C language, the octal number starts with a 0, and the hexadecimal number starts with a 0x.
You can also use type suffix with integer values to make it easier for the compiler to understand the type of the integer value. If you follow the value with
L, the compiler will treat it as a
long value, if you add
U after the value, the compiler treats the value as unsigned.
3500L, 14l // long value 99U, 89u // unsigned int value
Any valid floating-point value when used directly in a C program, is called a floating-point literal.
The floating-point numbers have a decimal part (fraction) or an exponential part.
100.50 0.000127 -0.77E-5
E-5 stands for 10-5
If we add a type suffix
f after the value, then the compiler will treat the value as
float type by default.
127.7F, 40f etc.
A valid character datatype value when used directly in the C program is called a character literal.
The character values are enclosed within a single quote.
'A', 'B', 'c', '#', etc.
A sequence of characters is called a String. We will learn about C strings in detail in the later section of this tutorial series.
A string literal is a value with multiple characters enclosed within double-quotes.
"study", "tonight", "c programming", etc.
Also known as Escape sequences, these combinations of characters with the backslash character
\ have special meaning for the C compiler.
A backslash character is used to escape characters with special meaning and make them normal characters for the compiler.
For example, if you use a single quotation mark, the compiler will think that it is the starting or end of a character value, but what if you want a single quote character.
'\'' \\ this is character with value '
In the example above the first and the last single quote marks the start and end of the character value, whereas the backslash escape character informs the compiler to treat the single quotation after it as a normal character.
The newline and horizontal tab code are quite commonly used, and you will be using them a lot as you start coding in the C language. So remember
\n is for newline and
\t is for the horizontal tab.
With this, we have covered all the Constants used in the C language which are also called Literals. Do not ignore the backslash escape literals as they are also very important and widely used in the C programs.