Literals in C programming

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.

  1. Integer literal
  2. Float or real literal
  3. Character literal
  4. String literal

Note: Literals are constant values not constant variables.

Learn – How to define constant variables in C.

Integer literal

In C programming we can specify an integer constant in three ways.

  1. Decimal number (base 10).
  2. Octal number (base 8).
  3. Hexadecimal number (base 16).

Integer literal is followed by an optional size or sign qualifier.
l or L is a size qualifier. It specifies size of an integer type as long.
u or 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 DecimalIn OctalIn HexadecimalDescription
2016u03740u0x7E0uUnsigned integer
2147483697l‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬0‭20000000061‬l‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬0x‭80000031‬l‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬Long integer
2147483697ul0‭20000000061‬ul‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬0x‭80000031u‬l‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬Unsigned long integer

Note: Order of the qualifiers doesn’t matter means ul or 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.

Decimal 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 + or - symbol representing negative or positive number.

Valid examples of float constants in decimal notation.
+1 1.2 -0.4 0. .3 -.3

Exponential notation

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

  1. Mantissa can be expressed either as decimal or as fractional number.
  2. We can use either uppercase or lowercase for exponent sign i.e. E or e.
  3. Exponent must be a decimal value.
  4. A + or - sign symbol may be prefixed before exponent and mantissa.
  5. Spaces are not allowed.

Character literal

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.

  1. Using character inside single quote. For example, ‘a’ ‘0’ ‘/’ ‘.’ etc.
  2. Using escape sequence characters. For example, ‘\n’ ‘\a’ ‘\b’ etc.
  3. Using an integer ASCII representing of a character. For example, 65 in ASCII format is used to represent ‘A’.
  4. Using octal or hexadecimal representation of integer as escape sequence character. For example, \05 \xA
  5. Using Unicode value of a character. For example, \u00A9

Multi-character constant

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 literals

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 + operator.

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.