C Language Constant and Literals
In this section, you will learn about Constant and Literals
Constant: A constant is a name given to the variable whose values can't be altered or changed. A constant is very similar to variables in the C programming language, but it can hold only a single variable during the execution of a program.
If you want to define a variable whose value cannot be changed, you can use the const keyword. This will create a constant.
For example,
const double PI = 3.14;
here const keyword is used to declare constant.
PI is a symbolic constant; its value cannot be changed.
const double PI = 3.14;
PI = 2.9; //Error
You can define constant using #define pre-processor directive.
#define PI 3.14
Literals:
Literals represent fixed values that cannot be modified. Literals occupies memory but can not be used like as variables. Generally, both terms, constants, and literals are used interchangeably.
C language has four type of Liteerals:
1. Integer literal
2. Float literal
3. Character literal
4. String literal
5.boolean literals
1. Integer Literals: Integer literals are numbers that do not have a decimal point or an exponential part. these type of literals are used to store or hold integer values only.
An integer literal can be a decimal, octal, or hexadecimal constant. A prefix specifies the base or radix: 0x or 0X for hexadecimal, 0 for octal, and nothing for decimal.An integer literal can also have a suffix that is a combination of U and L, for unsigned and long, respectively. The suffix can be uppercase or lowercase and can be in any order.
Here are some examples of integer literals −
2100 /* Legal */
2145u /* Legal */
0xGood /* Legal */
0898 /* Illegal: 8 and 9 is not an octal digit */
032UU /* Illegal: cannot repeat a suffix */
Types of integer Literals:
85 /* decimal */
0213 /* octal */
0x4b /* hexadecimal */
30 /* int */
30u /* unsigned int */
30l /* long */
30ul /* unsigned long */
2. Float Literals:
Floating point literals are the numbers they do have decimal point or exponent part.floating point literals can be represented either in decimal form or exponential form.
While representing literals in decimal form, we must include the decimal point, the exponent, or both; and when representing exponential form, we must include the integer part, the fractional part, or both. The signed exponent is introduced by e or E.
examples of floating-point literals −
3.14159 /* Legal */
314159E-5L /* Legal */
510E /* Illegal: incomplete exponent */
210f /* Illegal: no decimal or exponent */
.e55 /* Illegal: missing integer or fraction *
3. Character Literals: A Character Literal used to represent a single character.
A character literal contains a sequence of characters or escape sequences enclosed in single quotation mark symbols, for example 'c' . A character literal may be prefixed with the letter L, for example L'c' .
char type: It is used to store narrow-character literal.
A character literal can be a plain character (e.g., 'x'), an escape sequence (e.g., '\t'), or a universal character (e.g., '\u02C0').
4. String Literals:
A "string literal" is a sequence of characters set or escape sequences enclosed in double quotation marks ( " " ). String literals are used to represent a sequence of characters, which taken together form a null-terminated string.
eg.
"Best" /*string constant */
"" /* null string constant */
" " /*string constant of six white space */
"x" /*string constant having a single character. */
"Earth is round\n" /*prints string with a newline */
5. Boolean Literals:
Boolean literals allow only two values and thus are divided into two literals:
True: it represents a real boolean value
False: it represents a false boolean value
For example,
boolean b = true;
boolean d = false;
The boolean literals represent the logical value either true or false. These values are not case-sensitive they could be either in uppercase or lowercase and can be valid.