Constant values used within a program are known as Literals. These constant values occupy memory but do not have any reference like variables. Or as Wikipedia speaks literal is a notation for representing a fixed value within a source code.
There are four types of literals in C programming.
Note: Literals are constant values not constant variables.
In C programming we can specify an integer constant in three ways.
- Decimal number (base 10).
- Octal number (base 8).
- Hexadecimal number (base 16).
Integer literal is followed by an optional size or sign qualifier.
L is a size qualifier. It specifies size of an integer type as long.
U is a sign qualifier. It specifies an integer type as unsigned (can store only positive values).
Decimal constant is defined using digits between 0-9. For example: 123.
Octal constants is prefixed with 0 followed by digits between 0-7. For example: 0123 (in octal) equivalent to 83 in decimal. Always be cautious while defining octal constants, any number begins with 0 must only contain digits between 0-7 otherwise compiler will report an error on compilation. In addition, do not confuse octal constants as a decimal constant. For example: you might think 012 as a decimal constant 12, but it is an octal constant equivalent to 10 (in decimal).
Hexadecimal constant is prefixed with 0x or 0X following hexadecimal characters (i.e. digits from 0-9 and characters from a-f or A-F). For example: 0X1A or 0x1a or 0x1A (in hexadecimal) equivalent to 26 (in decimal).
Valid examples of defining integer constant
|In Decimal||In Octal||In Hexadecimal||Description|
|Unsigned long integer|
Note: Order of the qualifiers doesn’t matter means
lu both are same.
Do not get puzzled with qualifiers. I will cover them in depth in data types.
Float or real literal
In C programming a float or real constant is specified either using decimal or using exponential notation.
In decimal notation real constant is represented as whole number, followed by an optional decimal point and fractional part. It may be preceded with
- symbol representing negative or positive number.
Valid examples of float constants in decimal notation.
+1 1.2 -0.4 0. .3 -.3
Exponential notations are helpful in representing any number with very big or small magnitude. Numbers such as, 7850000000000 in exponential notation expressed as 7.85e12, 0.0000000000785 expressed as 7.85e-011.
In exponential notation real constants are expressed in scientific format specifying mantissa and exponent. Valid syntax of writing real constant in scientific format.
[+/-] <Mantissa> <e/E> [+/-] <Exponent>
Valid examples of real constants in exponential notation
0.1e1 0e-5 5e25 +5E+10 -5.2e0
Rules for representation of real constant in exponential notation
- Mantissa can be expressed either as decimal or as fractional number.
- We can use either uppercase or lowercase for exponent sign i.e. E or e.
- Exponent must be a decimal value.
-sign symbol may be prefixed before exponent and mantissa.
- Spaces are not allowed.
Character literal is a single character constant enclosed within single quotes. In C programming character constant occupies single byte space in memory.
There are many ways to represent a character constant in C programming.
- Using character inside single quote. For example, ‘a’ ‘0’ ‘/’ ‘.’ etc.
- Using escape sequence characters. For example, ‘\n’ ‘\a’ ‘\b’ etc.
- Using an integer ASCII representing of a character. For example, 65 in ASCII format is used to represent ‘A’.
- Using octal or hexadecimal representation of integer as escape sequence character. For example, \05 \xA
- Using Unicode value of a character. For example, \u00A9
Character constant containing more than one character inside single quote is known as multi-character constant. The compiler won’t complain if you give multi-character constant in single quote which is generally not practiced.
For example, 'abcd' is a valid character constant in C. However, it will only represent last character of the constant i.e. 'd' in our case. Multi-character constant is not considered as a good programming practiced and should be ignored.
String literal is a sequence of characters enclosed within double quotes. String constant occupies (total count of characters + 1) byte space in memory. The additional 1 byte is used for
\0 (NULL) character. A
NULL value is added explicitly at the end of the string to specify termination of the string. You can also concatenate string literals using
Examples of some valid string constants
"I love programming!"
I love Codeforwin.\n" + "I love India.
Important note: 'A' and "A" both looks similar but in real both are different. 'A' is a character constant occupying 1 byte space in memory. Whereas "A" is a string constant and occupies 2 byte space in memory.