A 2 5 Constants
This page is a generated reference surface for selective reading. It exists to keep the learner apps guide-first while still preserving source access.
Learning objectives
- Explain the main ideas and vocabulary in A 2 5 Constants.
- Work through the source examples for A 2 5 Constants without depending on raw chunk order.
- Use A 2 5 Constants as selective reference when learner modules point back to The C Programming Language.
Prerequisites
- None curated yet.
Module targets
module-01-c-programming-fundamentals
AI companion modes
- Explain simply
- Socratic tutor
- Quiz me
- Challenge my understanding
- Diagnose my confusion
- Generate extra practice
- Revision mode
- Connect forward / backward
Source-of-truth note
This unit is anchored to The C Programming Language and the source chapter "A 2 5 Constants". Use external resources only to clarify, extend, or modernize details without replacing the chapter's conceptual spine.
External enrichment
No chapter-specific enrichment resources are curated yet. Add them in the unit manifest when a source clearly improves learning.
Source provenance
- Primary source:
The C Programming Language - Source chapter: A 2 5 Constants
- Raw source file:
061-a-2-5-constants.md
Merged source
A 2 5 Constants
A.2.5 Constants
There are several kinds of constants. Each has a data type; Par.A.4.2 discusses the basic types:
constant:
integer-constant character-constant floating-constant enumeration-constant
A.2.5.1 Integer Constants
An integer constant consisting of a sequence of digits is taken to be octal if it begins with 0 (digit zero), decimal otherwise. Octal constants do not contain the digits8or9. A sequence of digits preceded by0xor0X(digit zero) is taken to be a hexadecimal integer. The hexadecimal digits includeaorAthroughforFwith values10through15.
An integer constant may be suffixed by the letter uorU, to specify that it is unsigned. It may also be suffixed by the letterlorLto specify that it is long.
The type of an integer constant depends on its form, value and suffix. (See Par.A.4 for a discussion of types). If it is unsuffixed and decimal, it has the first of these types in which its value can be represented: int, long int, unsigned long int. If it is unsuffixed, octal or hexadecimal, it has the first possible of these types:int, unsigned int, long int, unsigned long int. If it is suffixed by u or U, then unsigned int, unsigned long int. If it is suffixed bylorL, thenlong int, unsigned long int. If an integer constant is suffixed by
UL, it is unsigned long.
The elaboration of the types of integer constants goes considerably beyond the first edition, which merely caused large integer constants to belong. TheUsuffixes are new.
A.2.5.2 Character Constants
A character constant is a sequence of one or more characters enclosed in single quotes as in 'x'. The value of a character constant with only one character is the numeric value of the character in the machine's character set at execution time. The value of a multi-character constant is implementation-defined.
Character constants do not contain the'character or newlines; in order to represent them, and certain other characters, the following escape sequences may be used:
newline NL (LF) \n backslash \ \ horizontal tab HT \t question mark ? ?
vertical tab VT \v single quote ' ' backspace BS \b double quote " " carriage return CR \r octal number ooo \ooo formfeed FF \f hex number hh \xhh audible alert BEL \a
The escape\oooconsists of the backslash followed by 1, 2, or 3 octal digits, which are taken to specify the value of the desired character. A common example of this construction is\0(not followed by a digit), which specifies the character NUL. The escape \xhh consists of the backslash, followed byx, followed by hexadecimal digits, which are taken to specify the value of the desired character. There is no limit on the number of digits, but the behavior is undefined
if the resulting character value exceeds that of the largest character. For either octal or
hexadecimal escape characters, if the implementation treats thechartype as signed, the value is sign-extended as if cast to chartype. If the character following the \ is not one of those specified, the behavior is undefined.
In some implementations, there is an extended set of characters that cannot be represented in thechartype. A constant in this extended set is written with a precedingL, for exampleL'x', and is called a wide character constant. Such a constant has type wchar_t, an integral type
defined in the standard header<stddef.h>. As with ordinary character constants, hexadecimal
escapes may be used; the effect is undefined if the specified value exceeds that representable withwchar_t.
Some of these escape sequences are new, in particular the hexadecimal character representation.
Extended characters are also new. The character sets commonly used in the Americas and western
Europe can be encoded to fit in the char type; the main intent in adding wchar_t was to accommodate Asian languages.
A.2.5.3 Floating Constants
A floating constant consists of an integer part, a decimal part, a fraction part, an eor E, an optionally signed integer exponent and an optional type suffix, one off, F, l, or L. The integer and fraction parts both consist of a sequence of digits. Either the integer part, or the fraction part (not both) may be missing; either the decimal point or theeand the exponent (not both) may be missing. The type is determined by the suffix; Forfmakes it float, Lorlmakes it long double, otherwise it isdouble.
A2.5.4 Enumeration Constants
Identifiers declared as enumerators (see Par.A.8.4) are constants of typeint.
A.2.6 String Literals
A string literal, also called a string constant, is a sequence of characters surrounded by double quotes as in "...". A string has type ``array of characters'' and storage class static (see
Par.A.3 below) and is initialized with the given characters. Whether identical string literals are distinct is implementation-defined, and the behavior of a program that attempts to alter a string literal is undefined.
Adjacent string literals are concatenated into a single string. After any concatenation, a null byte\0is appended to the string so that programs that scan the string can find its end. String literals do not contain newline or double-quote characters; in order to represent them, the same escape sequences as for character constants are available.
As with character constants, string literals in an extended character set are written with a preceding L, as in L"...". Wide-character string literals have type ``array of wchar_t.''
Concatenation of ordinary and wide string literals is undefined.
The specification that string literals need not be distinct, and the prohibition against modifying them, are new in the ANSI standard, as is the concatenation of adjacent string literals. Wide-character string literals are new.
A.3 Syntax Notation
In the syntax notation used in this manual, syntactic categories are indicated byitalic type, and literal words and characters in typewriterstyle. Alternative categories are usually listed on separate lines; in a few cases, a long set of narrow alternatives is presented on one line, marked by the phrase one of.'' An optional terminal or nonterminal symbol carries the subscript opt,'' so that, for example,
{expressionopt }
means an optional expression, enclosed in braces. The syntax is summarized in Par.A.13.
Unlike the grammar given in the first edition of this book, the one given here makes precedence and associativity of expression operators explicit.