Constants
Constants are fixed values that do not change during the execution of a program. These fixed values are also known as literals.
Constants can be any of the basic data types, such as integer constants, floating-point constants, character constants, or string literals, as well as enumeration constants.
Constants are similar to regular variables, except that the value of a constant cannot be modified after its definition.
Integer Constants
Integer constants can be decimal, octal, or hexadecimal constants. The prefix specifies the base: 0x or 0X for hexadecimal, 0 for octal, and no prefix for decimal.
Integer constants can also have a suffix, which is a combination of U and L, where U represents an unsigned integer (unsigned) and L represents a long integer (long). The suffix can be in uppercase or lowercase, and the order of U and L is arbitrary.
Here are some examples of integer constants:
212 /* Valid */
215u /* Valid */
0xFeeL /* Valid */
078 /* Invalid: 8 is not an octal digit */
032UU /* Invalid: cannot repeat suffix */
The following are examples of integer constants of various types:
85 /* Decimal */
0213 /* Octal */
0x4b /* Hexadecimal */
30 /* Integer */
30u /* Unsigned integer */
30l /* Long integer */
30ul /* Unsigned long integer */
Floating-Point Constants
Floating-point constants consist of an integer part, a decimal point, a fractional part, and an exponent part. You can represent floating-point constants using decimal form or exponential form.
When using decimal form, you must include the integer part, the fractional part, or both. When using exponential form, you must include the decimal point, the exponent, or both. The signed exponent is introduced by e or E.
Here are some examples of floating-point constants:
3.14159 /* Valid */
314159E-5L /* Valid */
510E /* Invalid: incomplete exponent */
210f /* Invalid: no decimal or exponent */
.e55 /* Invalid: missing integer or fraction */
Character Constants
Character constants are enclosed in single quotes, for example, 'x' can be stored in a simple variable of type char.
Character constants can be a regular character (such as 'x'), an escape sequence (such as '\t'), or a universal character (such as '\u02C0').
In C, certain characters, when preceded by a backslash, have special meanings and are used to represent characters like the newline character (\n) or the tab character (\t). The following table lists some of these escape sequence codes:
Escape Sequence | Meaning |
---|---|
\ | \ character |
\' | ' character |
\" | " character |
\? | ? character |
\a | Alert or bell |
\b | Backspace |
\f | Form feed |
\n | Newline |
\r | Carriage return |
\t | Horizontal tab |
\v | Vertical tab |
\ooo | Octal number of one to three digits |
\xhh... | Hexadecimal number of one or more digits |
The following example shows some escape sequence characters:
Example
#include <stdio.h>
int main()
{
printf("Hello\tWorld\n\n");
return 0;
}
When the above code is compiled and executed, it produces the following result:
Hello World
String Constants
String literals or constants are enclosed in double quotes "". A string contains characters similar to character constants: regular characters, escape sequences, and universal characters.
You can break a long string constant using spaces as separators.
The following example shows some string constants. These three forms display the same string.
"hello, dear"
"hello, \
dear"
"hello, " "d" "ear"
Defining Constants
In C, there are two simple ways to define constants:
Using the #define preprocessor.
Using the const keyword.
#define Preprocessor
The following is the form to use the #define preprocessor to define constants:
#define identifier value
See the following example:
Example
#include <stdio.h>
#define LENGTH 10
#define WIDTH 5
#define NEWLINE '\n'
int main()
{
int area;
area = LENGTH * WIDTH;
printf("value of area : %d", area);
printf("%c", NEWLINE);
return 0;
}
When the above code is compiled and executed, it produces the following result:
value of area : 50
const Keyword
You can use the const prefix to declare constants with a specific type, as follows:
const type variable = value;
The const declaration of a constant must be completed in one statement:
See the following example:
Example
#include <stdio.h>
int main()
{
const int LENGTH = 10;
const int WIDTH = 5;
const char NEWLINE = '\n';
int area;
area = LENGTH * WIDTH;
printf("value of area : %d", area);
printf("%c", NEWLINE);
return 0;
}
When the above code is compiled and executed, it produces the following result:
value of area : 50
Note that it is a good programming practice to define constants in uppercase.