In this chapter, we will learn Constants & Literals in C.
Constant
Constant refers to that fixed values which may not alter during program execution. These fixed values are also called literals.
And Constants can be any of the basic data types like an integer constant, a floating constant, a character constant or a string literal. They are treated just like regular variable except their values cannot be modified after their definition.
Integer literals
An integer literal can be a decimal, hexadecimal or octal constant. A prefix which specifies the base or radix is 0x or 0X for hexadecimal, 0 for octal & nothing for decimal.
An integer literals can also have a suffix which is combination of U & L for unsigned & long. The suffix can be in any order & can be in Lower case or upper case.
Examples:
Here are some examples of string literals.

Below are other examples of various types of string literals:

Floating -point literals:
A floating -point literals has an integer part, a decimal part, a fractional part or an exponent part. We can represent floating point literals either in decimal form or in exponential form.
When representing decimal form, must include the decimal point, the exponent or both While representing exponential form must include the integer part, the fractional part or both. The signed exponent is denoted by e or E.
Some example for floating -point literals:

Character Constants
Character Constants/literals are enclosed in single quotes, e.g: ‘x’ can be stored in a single variable of char type.
The Character Constants can be a plain character (e.g. ‘x’), an escape sequence (e.g. ‘\t’), or a Universal character (e.g. ‘\u02C0’).
There are certain characters in C which represents special meaning when preceded by a backslash e.g. newline (\n) or tab (\t).
Here is the example to show few escape sequence characters:

String Literals:
String constants are enclosed in double quotes (” “). A String contains characters that are similar to characters literals: plain characters, escape sequence & Universal characters.
We can break a long line into multiple lines using string literals & separating them using White Spaces.
Here are some examples of string literals. All of the three forms are identical Strings.

Defining Constants
There are two ways to define constants in C.
- By using #define processor
- by using const keyword
The #define Processor
The form to use #define Processor to define constant is given below:

The below example will explain it in detail:

When you compile & execute the above code, then it will produce the following result:

The Constant Keyword
To declare constants with a specific type, you can use const prefix as follows:

The below example will explain it in detail:

When you compile & execute the above code, then it will produce the following result:

That’s the end of Constants & Literals in C.
Subscribe our channels: