Constants values are those values which can’t alter or change during program execution. These fixed (constants) value are also known as literals. Constants can be any data types like an integer constant, a floating constant, a character constant, or string literal.
Integer literal:
An integer literal is a sequence of digits and denotes a number. An integer literal is either signed or unsigned. The sign of a literal determines how C– decides whether it stats into its type:
- A literal starting with “0x” or “0X” is in hexadecimal notation (base 16) and is unsigned.
- A literal starting with ‘0’ (zero) is in octal notation (base 8) and is unsigned.
- A literal starting with any of the digits 1 through 9 and ending in the letter “u” or ‘U’ is in decimal notation (base 10) and is unsigned. Also, a literal ‘0u’ or ‘0U’ is in decimal notation and is unsigned.
- A literal starting with any of the digits 1 through 9 and ending in a digit is in decimal notation (base 10) and is signed.
- A literal starting with a minus sign, followed by any of the digits 1 through 9, and ending in a digit, is in decimal notation (base 10) and is signed.
Examples:
2 3 4 5 6 7 8 9 10 |
85 /* decimal */ 0213 /* octal */ 0x4b /* hexadecimal */ 30 /* int */ 30u /* unsigned int */ 30l /* long */ 30ul /* unsigned long */ |
Floating-point literal:
A floating-point literal denotes a floating-point number. It has an integer part, a decimal point, a fractional and an exponent part. It is a checked compile-time error when the floating-point literal can’t be represented by k bits where ‘k’ is the native word size. A floating-point literal may not begin with a dot.
Example:
2 3 4 5 6 7 8 |
3.14159 /* Legal */ 314159E-5L /* Legal */ 510E /* Illegal: incomplete exponent */ 210f /* Illegal: no decimal or exponent */ .e55 /* Illegal: missing integer or fraction */ |
Character literals:
A character literal is a value of type bits 8 by default. It is specified as an ASCII character enclosed in single quotes:’a’ denotes the decimal value 97 (ASCII code for ‘a’). A character literal can be a plain character (e.g., ‘x’), an escape sequence (e.g., ‘\t’), or a universal character (e.g., ‘\u02C0’).
2 3 4 5 6 7 8 9 10 11 |
#include <stdio.h> int main() { printf("Hello\tWorld\n\n"); return 0; } |
After execution of above , we will get below result:
2 3 4 |
Hello World |
String literal:
A string literal is an abbreviation for a sequence of character literals and can be used only to define the initial value of data. A String literal is terminated by double quotes and must contain only printable characters. Each character is value of type bits 8. You can break a long line into multiple lines using string literals and separating them using white spaces.
2 3 4 5 6 7 8 9 10 |
"hello, codepully" "hello, \ codepully" "hello, " "code" "pully" |
Pradeep Maurya is the Professional Web Developer & Designer and the Founder of “Tutorials website”. He lives in Delhi and loves to be a self-dependent person. As an owner, he is trying his best to improve this platform day by day. His passion, dedication and quick decision making ability to stand apart from others. He’s an avid blogger and writes on the publications like Dzone, e27.co