Chapter 2 Summary and Quiz
Recap of tokens, types, sizes, constants, and storage class keywords.
The Vocabulary Is Now Yours
This chapter was about precision with the language's raw material: what the compiler reads, what types actually promise, how constants are spelled, how a whole set of related constants earns a name, and the four words that govern where variables live. These are the chapters exams quote most literally, so the recap below doubles as a revision sheet.
Tokens, Keywords, Identifiers
The compiler chops source into tokens of six kinds: keywords, identifiers, constants, string literals, operators, punctuation. ANSI C reserves exactly 32 keywords, all lower case. Identifiers use letters, digits, and underscore; no leading digit; case-sensitive throughout; leading underscores belong to the compiler and library by convention. Whitespace separates tokens and does nothing else.
Types and Their Real Sizes
A type is a size plus an interpretation of the bits. The basic four are char, int, float, double, with short, long, signed, unsigned modifying the integer family. The standard's promises are minimums, not sizes: char exactly 1 byte, int at least 16 bits, long at least 32, never shrinking up the ladder. The textbook's "-32768 to 32767" describes a 2-byte int on a 16-bit compiler; this platform's int is 4 bytes. sizeof measures; <limits.h> names each integer type's range and <float.h> each floating type's, so programs never hardcode either.
In the vocabulary exam papers use, those are the primary types, along with void; derived types are the ones built from others, meaning arrays, pointers, and functions; user-defined types are the ones you name yourself with enum, struct, union, and typedef.
char is a small integer wearing a character costume: 'A' is 65, %c prints the costume, %d the number. double beats float as the default: the language's literals and library are built around it. Above double sits long double, the most machine-dependent type in the language, whose constants take an L and whose values print with %Lf.
Constants in Every Notation
Integer constants come in three bases: decimal, octal with a leading 0 (the 010-is-eight trap), hexadecimal with 0x. Suffixes U and L pick types. Floating constants are double unless suffixed F for float or L for long double, and may use exponent form (2.5e3). Character constants are single-quoted and integral; '0' is 48, not 0, and '\0' is the zero-code character that strings will be built on.
Escape sequences are one character each, written with a backslash: \n \t \v \b \r \f \a \\ \' \" \? \0, with \ooo and \xhh for a code given in octal or hexadecimal. \\ is the compiler's business; the doubled %% that prints a percent sign is printf's.
Three ways to name a fixed value:
const double rate = 7.5;declares a real, typed variable that rejects later assignment, enforced at compile time.#define PI 3.14159tells the preprocessor to substitute text before compilation: no type, no memory, no semicolon, UPPER_CASE by convention.enum { MAX_STUDENTS = 50 };declares a genuine integer constant expression, which is what the places demanding one, array sizes above all, will accept.
Prefer const for typed values, an enum constant for integers that must be constant expressions, and #define for the non-integer cases and for names that must exist before compilation. On exams, cite the deepest difference: substitution versus protected variable.
const has a partner qualifier, volatile, which forbids the compiler from caching or discarding reads of an object that something outside the program may change. It is a type qualifier and not a storage class, which is the form the trick question takes, and it promises nothing about atomicity.
Enumerations
enum weekday { MONDAY, TUESDAY, WEDNESDAY }; declares a type whose name is enum weekday, both words always, plus constants numbered from zero in declaration order. An explicit = value sets one constant and the next continues from it, so { A = 1, B, C = 10, D } yields 1, 2, 10, 11. The type and its variables may be declared in one statement, and a tagless enum { NAME = value }; declares constants and nothing else.
The honest part, and the exam-worthy one: an enumeration constant has type int, and C checks nothing. Storing 99 in a variable whose set has five values compiles silently, so an enumeration is documentation the compiler helps you write rather than a guarantee it enforces. Prefix constants with the set's name, because they share the scope around them rather than getting one of their own.
The Four Storage Classes
auto is the unwritten default for locals: fresh each block entry, indeterminate until initialized. static lives for the whole program, keeps its value between visits, and defaults to zero. extern declares a variable defined elsewhere. register is an ignored speed hint whose one effect is that the variable has no address. Four classes; chapter 9 makes them concrete.
One Program, Whole Chapter
marathon: 42.20 km
initial K is code 75
int: 4 bytes, max 2147483647
4 distances, marathon is code 3
Every line uses this chapter: a symbolic constant, a const variable, a char printed both ways, sizeof into an unsigned long, a <limits.h> constant, and an enumeration whose automatic numbering supplies both the marathon's code and the count of distances. If each line reads as obvious, take the quiz.
Looking Ahead
Chapter 3 puts the values to work: every operator family in C, the precedence rules that decide what computes first, and the first encounters with expressions whose result the standard refuses to define.
How did you find this lesson?
Your rating helps us improve the content.
Create an account to track your progress and access interactive exercises. Already have one? Sign in.
Chapter 2 Summary and Quiz - Quiz
Test your understanding of the lesson.
Lesson Discussion
Share your thoughts and questions
No comments yet. Be the first to share your thoughts!