C Fundamentals: Character Set, Tokens, Identifiers, Keywords, Data Types, Constants, Variables and Declarations
This domain covers the raw material every C program is built from: the character set, how the compiler splits source text into tokens, the rules for naming things, the reserved words, and how data types, constants, variables and declarations fit together. KSEAB questions here are short and definitional, but they punish small vocabulary mistakes.
Tokens and identifiers
A C program is a sequence of tokens: keywords, identifiers, constants, string literals, operators and punctuators. Whitespace and comments separate tokens and are otherwise ignored, so the layout of source text is free-form. An identifier begins with a letter or underscore, continues with letters, digits or underscores, is case sensitive, and must not be a keyword. sum, _count and Total2 are legal; 2total, total-sum and float are not.
Keywords and data types
Standard C (C89) has 32 keywords, including auto, char, const, double, enum, extern, float, goto, int, register, return, short, signed, sizeof, static, struct, switch, typedef, union, unsigned, void, volatile and while. C99 adds inline, restrict, _Bool, _Complex and _Imaginary. The basic types are char, int, float, double and void, modified by short, long, signed and unsigned. sizeof(char) is always 1; the width of int is implementation-defined, so portable code consults <limits.h>.
Constants and declarations
Integer constants may be decimal (42), octal (leading zero, 052) or hexadecimal (0x2A). A character constant such as 'A' has type int; the string "A" is an array of two char because of the terminating '\0'. A declaration names an object, gives it a type and may initialise it: int a = 5, b;. An uninitialised local variable holds an indeterminate value.
Worked example
int x = 010 + 0x10; gives 24, because 010 is octal 8 and 0x10 is 16. Reading a leading zero as decimal is the classic error.
Traps
08and09are invalid; octal digits stop at 7."a"is not'a'- different types and sizes.- Keywords are lowercase:
Int,FloatandMainare ordinary identifiers. extern int n;declares;int n;defines.