Constants in C Programming

0
In C programming, constants are fixed values that do not change during the execution of a program. They are categorized into four types: integer constants, floating constants, enumeration constants, and character constants.

Integer Constants

An integer constant begins with a digit and does not have a period or exponent part. It may have a prefix that specifies its base and a suffix that specifies its type. The value of an integer constant is computed based on its base: base 10 for decimal constants, base 8 for octal constants, and base 16 for hexadecimal constants. The type of an integer constant is determined by its value and suffix.

int a = 10; // decimal constant
int b = 012; // octal constant
int c = 0xA; // hexadecimal constant

Floating Constants

A floating constant has a significand part that may be followed by an exponent part and a suffix that specifies its type. The significand part is interpreted as a (decimal or hexadecimal) rational number; the digit sequence in the exponent part is interpreted as a decimal integer. The type of a floating constant is determined by its suffix: double for unsuffixed constants, float for constants suffixed by 'f' or 'F', and long double for constants suffixed by 'l' or 'L'.

double d = 1.23; // decimal floating constant
float e = 0x1.2p3f; // hexadecimal floating constant

Enumeration Constants

An enumeration constant is an identifier declared as an enumeration constant and has type int.

enum Weekdays {Mon, Tue, Wed, Thu, Fri, Sat, Sun};
Weekdays today = Mon;

Character Constants

A character constant is a sequence of one or more multibyte characters enclosed in single quotes. A wide character constant is the same, except prefixed by the letter 'L', 'u', or 'U'. The value of a character constant is the numerical value of the representation of the mapped character interpreted as an integer.

char f = 'a'; // character constant
wchar_t g = L'あ'; // wide character constant
Tags

Post a Comment

0Comments
Post a Comment (0)