-
Notifications
You must be signed in to change notification settings - Fork 0
Constants_and_Literals
The constants refer to fixed values that the program cannot alter during its execution. These same fixed values are also called literals.
Constants (keyword const
) can be any basic data types like constant integer (const int
), constant floating (const float
), constant character (const char
), and so on. It is also possible for enumerations do be constant as well.
Overall, the constants are treated pretty much like regular variables, the only difference being their values cannot be modified after their initialization and definition.
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 without any difference during program execution.
Here are some examples of integer literals:
212 /* Legal */
215u /* Legal */
0xFeeL /* Legal */
078 /* Illegal: 8 is not an octal digit */
032UU /* Illegal: cannot repeat a suffix */
Here are also other examples of various types of integer literals:
85 /* decimal */
0213 /* octal */
0x4b /* hexadecimal */
30 /* int */
30u /* unsigned int */
30l /* long */
30ul /* unsigned long */
Floating point literals are numbers that have a decimal point or an exponential part. They can be represented as:
- Real literals
- Binary floating point literals
- Hexadecimal floating point literals
- Decimal floating point literals (IBM extension)
- Complex literals
A real binary floating point constant consists of the following:
- an integral part
- a decimal point
- a fractional part
- an exponent part
- an optional suffix
Both the integral and fractional parts are made up of decimal digits. You can omit either the integral part or the fractional part, but not both. You can omit either the decimal point or the exponent part, but not both.
The following examples are floating point literals:
5.3876e4 /*53876*/
4e-11 /*0.00000000004*/
1e+5 /*100000*/
7.321E-3 /*0.007321*/
3.2E+4 /*32000*/
0.5e-6 /*0.0000005*/
0.45 /*0.45*/
6.e10 /*60000000000*/
The suffix f
or F
indicates a type of float, and the suffix l
or L
indicates a type of long double. If a suffix is not specified, the floating point constant has a type double.
A plus +
or minus -
symbol can precede a floating point literal. However, it is not part of the literal; it is interpreted as a unary operator.
A real hexadecimal floating point constant which are a C99 feature, consists of the following:
- a hexadecimal prefix
- a significant part
- a binary exponent part
- an optional suffix
The significant part represents a rational number and is composed of the following:
- a sequence of hexadecimal digits (whole-number part)
- an optional fraction part
The optional fraction part is a period followed by a sequence of hexadecimal digits.
The exponent part indicates the power of 2 to which the significant part is raised, and is an optionally signed decimal integer. The type suffix is optional.
Here is an example:
0x12.2P2 /* 0x12.2P2 is 0x122/16 * 2^2 = 72.5 */
The suffix f
or F
indicates a type of float, and the suffix l
or L
indicates a type of long double. If a suffix is not specified, the floating point constant has a type double.
A real decimal floating point constant consists of the following:
- an integral part
- a decimal point
- a fractional part
- an exponent part
- an optional suffix
Both the integral and fractional parts are made up of decimal digits. You can omit either the integral part or the fractional part, but not both. You can omit either the decimal point or the exponent part, but not both.
The suffix df
or DF
indicates a type of _Decimal32, the suffix dd
or DD
indicates a type of _Decimal64, and the suffix dl
or DL
indicates a type of _Decimal128. If a suffix is not specified, the floating point constant has a type double.
The following are examples of decimal floating point literal declarations:
_Decimal32 a = 22.2df;
_Decimal64 b = 33.3dd;
Complex literals, which are a C99 feature, are constructed in two parts: the real part, and the imaginary part.
The floating point constant can be specified as a decimal or hexadecimal floating point constant (including optional suffixes), in any of the formats described in the previous sections.
_Complex_I is a macro defined in the complex.h
header file, representing the imaginary unit i, the square root of -1.
For example, the declaration:
varComplex = 2.0f + 2.0f * _Complex_I;
initializes the complex variable varComplex to a value of 2.0 + 2.0i.
For ease of porting applications developed with GNU C, XL C also allows you to indicate the imaginary part of a complex literal with a suffix, in addition to the standard suffixes that indicate the type of the complex number (float, double, or long double).
For example, the declaration
varComplex = 3.0f + 4.0fi;
initializes the complex variable varComplex to a value of 3.0 + 4.0i.
Character literals are enclosed in single quotes, e.g., 'x'
and can be stored in a simple variable of char type.
A character literal 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 when they are preceded by a backslash they will have special meaning and they are used to represent like newline \n
or tab \t
. Here, you have a list of some of such escape sequence codes:
Operator | 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 |
Following is the example to show few escape sequence characters:
#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 literals or constants are enclosed in double quotes "". A string contains characters that are similar to character literals: plain characters, escape sequences, and universal characters.
You can break a long line into multiple lines using string literals and separating them using whitespaces.
Here are some examples of string literals. All the three forms are identical strings.
"hello, dear"
"hello, \
dear"
"hello, " "d" "ear"
There are two simple ways in C to define constants:
- Using #define preprocessor
- Using const keyword
Following is the form to use #define preprocessor to define a constant:
#define identifier value
For example:
#define PI 3.141592
Following example explains it more thoroughly:
#include <stdio.h>
#define LENGTH 10
#define WIDTH 5
#define NEWLINE '\n'
int main()
{
int area;
area = LENGTH * WIDTH;
printf("value of area:%c%d", NEWLINE, area);
return 0;
}
When the above code is compiled and executed, it produces the following result:
value of area:
50
You can use const prefix to declare constants with a specific type as follows:
const type variable = value;
For example:
const float pi = 3.141592;
Following example explains it in detail:
#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:%c%d", NEWLINE, area);
return 0;
}
When the above code is compiled and executed, it produces the following result:
value of area:
50
Soon...