Title: Theory / Unimportant / CS / Programming / C Harbison Steele{ / 03 Preprocessor+14
Tags: récupéré seen_description FromInstruction computer_to_instruction hasNotNewEffectReturnsMeaning declaration automatedNewLineAfterCommaSpace todo_declaration
On every note of this deck, unless a note says otherwise:
Chapter: 3

Name: Preprocessor
Definition: simple macro processor that conceptually processes the source text of a C program before the compiler proper reads the source program.
Page: 43
Chapter: 3

Instruction: # (whitespace and newline)
Page: 45
Effect: null directive
Language: C
Section: 2

Instruction: #define foo bar
Page: 43, 46
Meaning: each occurrence of foo in the following text will be replaced by bar
Language: C
Chapter: 3
Section: 1, 3.1
Identifier: foo
Name: objectlike macro definition
Assuming: either foo is undefined, or the definition is identical on token level
Example: isolate implementation dependant restrictions

Instruction: #define foo(
  bar_1,
  ...,
  bar_n,
  ...
) code (
  the last ... is a literal sequence of ...
)
Page: 58
Meaning: in code, __VA_ARGS__ is replaced by the merging of bar_n+1,
  ...,
  bar_m
Language: C
Chapter: 3
Section: 1,
  3.10
Context: macro
Identifier: foo
Assuming: m>n
Similar to: #define foo(
  bar_1,
  ..., bar_n,
  ...
) code

Instruction: #define foo(
  bar_1,
  ...,
  bar_n
) code
Page: 43,
  47
Meaning: each occurence of foo(
  buz_1,
  ..., buz_n
) in the text will be replaced by zin, where bar_i is replaced by buz_i
Language: C
Chapter: 3
Section: 1,
  3.2
Context: macro
Identifier: foo
Assuming: either foo is undefined,
  or the definition is identical on token level
no space before (

Instruction: #undef foo
Page: 44
Meaning: Remove the preprocessor macro definition of foo
Language: C
Chapter: 3
Section: 1
Assuming: foo is not a macro mandatory by the standard

Instruction: #include <foo>
Page: 60
Effect: Insert text from the source file foo. foo is searched in directories according to some rules
Language: C
Section: 4

Instruction: #include "foo"
Page: 60
Effect: Insert text from the source file foo. foo is searched locally first, and then in directories according to some rules
Language: C
Section: 4

Instruction: #if foo_1
bar_1
...
#elif foo_n
bar_n
#else
plop
#endif
Page: 61-62
Effect: bar_i if foo_i is the frist true constant from the list of foo_j
plop otherwise
Language: C
Section: 5.1

Instruction: #ifndef foo
bar
#else
plop
#endif
Page: 61-62
Effect: bar if foo is a macro defined
plop otherwise
Language: C
Section: 5.1

Instruction: #ifdef foo
bar
#else
plop
#endif
Page: 61-62
Effect: bar if foo is a macro defined
plop otherwise
Language: C
Section: 5.1

Instruction: #ifdef
Page: 44
Effect: Conditionnaly include some text based on whether a macro name is defined
Language: C
Context: Macro
Section: 1

Instruction: #ifndef
Page: 44
Effect: Conditionnaly include some text based on whether a macro name is not defined
Language: C
Section: 1

Instruction: #else
Page: 44
Effect: Conditionnaly include some text based on whether the last if failed
Language: C
Section: 1

Instruction: #endif
Page: 44
Effect: terminate conditional text
Language: C
Section: 1

Instruction: #line
Page: 44
Effect: supply a line number for compiler message
Language: C
Section: 1

Instruction: #elif
Page: 44
Effect: if last if failed, use this other one
Language: C
Context: macro
Section: 1

Instruction: defined
Page: 44
Effect: Preprocessor function that yields 1 if a name is defined as a preprocessor macro and 0 othelrwise;
Language: C
Section: 1
Where to use it: preprocessor if

Instruction: # operator
Page: 44
Effect: Replace a macro parameter with a string constant containing the parameter's value.
Language: C
Section: 1

Instruction: foo##bar
Page: 56
Effect: foobar is now a single token
Language: C
Section: 3.9
Returns: x##3 gives x3, allowing to create variable names parametered by an int
Where to use it: Macro expansion

Instruction: #fpragrma
Page: 44
Effect: Specify implementation-dependent infonnation to the compiler.
Language: C
Section: 1

Instruction: #error
Page: 44
Effect: Produce a compile-time error with a designated message.
Language: C
Section: 1

Instruction: \ (newline)
Page: 44
Effect: newline is ignored
Language: C
Section: 2

Question: For macro function, restriction on ( in arguments
Answer: parenthesis must be propertly balanced
Page: 48
Chapter: 3
Section: 3.2

Question: For macro function, restriction on the commas "," occurring in arguments
Answer: it must appear in some parenthesis
Page: 48
Chapter: 3
Section: 3.2

Question: Restriction on string literals in arguments
Answer: The ",", "(" and ")" are ignored
Page: 48
Chapter: 3
Section: 3.2
Context: macro function

Question: How to define a macro, which do foo, and consume a semicolon.
Answer: do {foo} while (0)
Page: 49
Chapter: 3
Section: 3.2

Question: In case of recursive macro expression, in which order are they expanded
Answer: More external macro first. Its result is scanned to find the potential other macro expression
Page: 50
Chapter: 3
Section: 3.3

Instruction: __LINE__
Page: 51
Effect: Macro replaced by the line of the file where it appears
Language: CC
Section: 3.4

Instruction: __FILE__
Page: 51
Effect: Macro replaced by the name of the file where it appears
Language: C
Section: 3.4

Instruction: __DATE__
Page: 51
Effect: Macro replaced by the compilation date
Language: C
Section: 3.4

Instruction: __TIME__
Page: 51
Effect: Macro replaced by the compilation TIME
Language: C
Section: 3.4

Instruction: __func__
Page: 51
Effect: Name of the enclosing function
Language: CC
Section: 3.4

Instruction: __STDC__
Page: 51
Effect: The decimal constant 1 if and only if the compiler is an ISO-confonning implementation.
Language: C
Section: 3.4

Instruction: __STDC_VERSION__
Page: 52
Effect: A number choosen by the version to which this implemantion conform
Language: C
Section: 3.4
Index: 2
Kind: Table

Instruction: __STDC_HOSTED__
Page: 52
Effect: 1 for hosted implementation, 0 otherwise
Language: C
Section: 3.4
Index: 2
Kind: Table

Instruction: __STDC_IEC_559__
Page: 52
Effect: 1 if floating point implementation conforms to IEC 60559, 0 otherwise
Language: C
Section: 3.4
Index: 2
Kind: Table

Instruction: __STDC_IEC_559_Complex__
Page: 52
Effect: 1 if complex arithmetic implementation conforms to IEC 60559, 0 otherwise
Language: C
Section: 3.4
Index: 2
Kind: Table

Instruction: __STDC_IEC_10646__
Page: 52
Effect: yyyymmL for the year and month of the ISO implementation to which it conforms
Language: C
Section: 3.4
Index: 2
Kind: Table

Instruction: unix
Page: 52
Effect: if defined, it is compiled for unix, otherwise undefined
Language: C
Section: 3.4
Kind: macro

Question: In some old implementation, #defined and #undef are
Answer: a stack of definition, such that undefining allows last definition to be back
Page: 4
Chapter: 3
Section: 3.5
Kind: macro

Question: Good habits to deal with macro
Answer: parenthesis each parameter
Page: 54, 55
Chapter: 3
Section: 3.6, 3.7
Answer2: parenthesis total body if it is an expression
Kind: macro
Answer3: if a parameter is used many time, put its value in a variable, in order to avoid doing side effect twice

Instruction: #foo
Page: 56
Effect: the value of the argument foo is replaced by "foo", with white space trimmed at begin, end, sequence of whitespace replaced by a single one, and " and \ are preceded by a \
Language: C
Context: Macro body
Section: 3.8

Question: What append if a macro name is in a string ?
Answer: It is ignored
Page: 54
Chapter: 3
Section: 3.8

Instruction: __VA_ARGS__
Page: 58
Effect: the merging of the extra arguments at the end of the macro call
Language: C
Context: macro body
Section: 3.10
Intuition: variable argument

Question: foo##bar
Answer: create a token which is the compositions of the token foo and bar
Page: 56
Chapter: 3
Section: 3.9
Context: macro definition

Question: What occurs in foo##bar if one of the token is empty
Answer: it is considered to be the empty token, and thus this operation is ignored
Page: 56
Chapter: 3
Section: 3.9
Context: macro definition

Instruction: foo##bar
Page: 56
Effect: create a token which is the compositions of the token foo and bar
Language: C
Context: macro definition
Section: 3.9

Name: variable arguments
Definition: The variables which are not named, but at the position of ...
Page: 56
Chapter: 3
Section: 3.10
Context: macro function definition

Instruction: #define foo(
  bar_1,
  ...,
  bar_n,
  ...
) plop
Where the last ... is textually here
Page: 56
Meaning: Similar to
#define foo(
  bar_1,
  ...,
  bar_n,
  ...
) plop
where the arguments after the n are put in __VA_ARGS__
Language: C
Chapter: 3
Section: 3.10
Context: macro function definition

Instruction: __VA_ARGS__
Page: 56
Language: C
Context: macro function definition
Section: 3.10
Returns: The arguments in a macro function definitions which have no name

Instruction: #include foo_1 ... foo_n
Page: 59
Effect: Apply macro replacement, and then act as a normal #include
Language: C
Section: 4

Name: Standard header
Definition: Header which can be included in any C implementation.
Page: 61
Chapter: 3
Section: 4

Instruction: #line foo bar
Page: 66
Effect: Preprocessor believes now that it is in file bar in line foo
Language: C
Section: 6

Instruction: #line n
Page: 66
Effect: Preprocessor believes now that it is in line n
Language: C
Section: 6
Variables: (n an integer)

Instruction: #line foo (foo not an integer)
Page: 66
Effect: Apply macro replacement on foo and act as another #line
Language: C
Section: 6

Instruction: __FILE__
Page: 66
Effect: The name of the current file
Language: C
Section: 6

Instruction: __LINE__
Page: 66
Effect: The current line
Language: C
Section: 6

Instruction: #pragma foo or _Pragma("foo")
Page: 67
Effect: State to use a compiler dependant rule named foo
Language: C
Section: 7

Instruction: STDC
Page: 68
Effect: State that the pragma belongs to standard C
Language: C
Section: 7.1

Question: In which part of the program a pragma holds
Answer: if it is defined in a compound statement, then it remains true until its end
Page: 68
Chapter: 3
Section: 7.2
Context: C
Answer2: Otherwise, it remains true until another pragma statement cancel it

Question: The scope of an identifier is
Answer: limited to a C source file by default
Page: 75
Chapter: 4
Section: 2.1
Context: C

Question: The scope of an external identifier is
Answer: every file
Page: 75
Chapter: 4
Section: 2.1
Context: C

Name: d is visible from i
Definition: a use of the identifier in that context will be bound to the declaration
Page: 76
Chapter: 4
Section: 2.2
Variables: i an identifier
d a declaration
Context: C

Question: When can an identifier may be redeclared
Answer: If the last declaration is not in the current function definition.
Page: 76
Chapter: 4
Section: 2.2
Context: C
Answer2: If the last declaration is in another file

Name: The declaration point of i
Definition: The end of the declarator that contains the identifier's lexical token
Page: 76
Chapter: 4
Section: 2.3
Variables: i an identifier
Context: C
Definition2: Where i can start to be used

Question: In
static int intsize=sizeof(intsize);
where is the declaration point ?
Answer: After the first occurrence of intsize
Page: 76
Chapter: 4
Section: 2.3
Context: C

Question: In
typedef struct S { int Value; struct S *Next; } cell;
where is the declaration point of cell?
Answer: After the last occurrence of cell
Page: 77
Chapter: 4
Section: 2.3
Context: C

Name: An overloaded name
Definition: A name which is associated with more than one program entity at a time
Page: 77
Chapter: 4
Section: 2.4
Context: C

Name: overloading classes
Definition: Preprocessor macro names
Page: 77
Chapter: 4
Section: 2.4
Context: C
Definition2: Statement labels
Conjdef: A dictionnary from name to declarations
Definition3: Structure union and enumeration tags
Name2: Name space
Definition4: Component names ("Members" in standard C)
Definition5: Other names

Name: Preprocessor macro names
Definition: A name used by preprocessor, and not the actual C program
Page: 78
Chapter: 4
Section: 2.4
Context: C

Name: Statement label
Definition: A name declared as the left part of a : which is not part of case label
Page: 78
Chapter: 4
Section: 2.4
Context: C

Name: Structure, union and enumeration tags
Definition: Tags which are part of structure, union and enumeration type specifier, declared after struct, union or enum
Page: 78
Chapter: 4
Section: 2.4
Context: C

Name: component names
Definition: Name associated with each structure and union type.
Page: 78
Chapter: 4
Section: 2.4
Context: C
Definition2: They follow . or ->
Name2: Members name

Name: Conflicting declaration
Definition: with the same name,
Page: 78
Chapter: 4
Section: 2.5
Context: C
Definition2: in the same block/toplevel
Example: extern int howmany;
typedef double howmany();
Conjdef: Declarations
Definition3: in the same name space
Definition4: either not external, or with different type
Counterexample: extern char str[10];
extern struct str {int a,b}
X;

Name: Duplicate visibility of name
Definition: A name, occurring at least twice in a block, referencing to two distinct declarations
Page: 79
Chapter: 4
Section: 2.6
Context: C
Example: {
  int i =0;
  B:{
    int j =i;
    float i=10.0;
  }
}

Name: The extent of v
Definition: the period of time that the storage of v is allocated
Page: 79
Chapter: 4
Section: 2.7
Variables: v a variable
Context: C
Name2: The lifetime of v
Name3: The storage duration of v

Name: An object with static extent
Definition: An object which is allocated at or before the beginning of program execution, and the storage remains allocated until program termination
Page: 79
Chapter: 4
Section: 2.7
Context: C
Example: variable in toplevel
Example2: Function
Example3: Variables declared static

Name: An object with local extent
Definition: An object which is allocated on entry to a block or function and is destroyed on exit from the block or function
Page: 79
Chapter: 4
Section: 2.7
Context: C
Name2: An automatic variable

Name: A dynamic object
Definition: A object created and destructed explicity in the code
Page: 79
Chapter: 4
Section: 2.7
Context: C

Name: An initializer of v
Definition: Expression used to set the initial value of a variable at the time that storage is allocated for it
Page: 79
Chapter: 4
Section: 2.8
Variables: v a variable
Context: C

Question: The initial value of v is
Answer: 0 if v is static
Page: 79
Chapter: 4
Section: 2.8
Variables: v a variable
Context: C
Answer2: unpredictable otherwise