C8: C Pre-Preprocessor Flashcards
What can the pre-processor do?
It can
- include header files,
- include external files through use of directives,
- define symbolic constants and macros,
- do conditional compilation, and
- handle pre-processor directives.\
- process tokens - replace occurrences of text in file
What are the two forms of preprocessor directives for including external files?
#include • For standard library header files • Searches predesignated directories #include "filename" • Normally used for programmer-defined files • Searches in current directory
Name two ways to declare constants in C. Which way uses the preprocessor?
- const variables (ex: const int PI = 3.14;)
- symbolic constants (ex #define PI). PI is called an identifier and the whole definition is a processor directive. The identifier is not seen by debugger, has no specific data type and no scope
What is a macro in C?
A macro is an operation defined in preprocessor directive. It looks like a function but performs a text substitution. ex: #define CIRCLE_AREA(r) You can call the macro like a function in the code: area = CIRCLE_AREA(4);
What is conditional compilation (preprocessor directive)?
With conditional compiliation, you can check whether another identifier is defined. Ex: #if defined(PI) #elif ... #endif
#ifdef PI #elif ... #endif are identical.
Or: (Header Guard in .h file) #ifndef LIST_H #define LIST_H ... (code) ... #endif
What is the use of a header guard in preprocessing?
Use: to avoid including a header file more than once, check if it has already been included. Ex: grandparent.c #ifndef GRANDPARENT_H #define GRANDPARENT_H struct foo{ int member; }; #endif ....
in parent.c #ifndef PARENT_H #define PARENT_H #include "grandparent.h" #endif
What is a token?
A token is an occurrence of text in a file. (not the strings in the code, .. the actual text of the code.)
What is the # operator/
macro that causes the corresponding actual argument to be enclosed in double quotations marks. ex: #define MKSTR(s) #s int main(void){ printf(MKSTR(geeksforgeeks)); return0; } -output geeksforgeeks (as a string)
What does the # operator do?
It causes the corresponding actual argumetn to be enclosed in double quotation marks: ex: #include #define MKSTR(s) #s int main(void){ printf(MKSTR(geeksforgeeks)); return 0; } it will print "geeksforgeeks" as string: geeksforgeeks
What are the predefined symbolic constants in C?
\_\_LINE\_\_ = line number of the current source code line ; integer constant \_\_FILE\_\_ = presumed name of source file \_\_DATE\_\_ = date of compilation \_\_TIME\_\_ = time of compilation
What is a symbolic constant?
It is a constant that is passed in through preprocessor with #define. Ex: #define PI 3.14159 . But, these constants are not seen by debugger, do not have a specific data type and have no scope.
What is a macro?
A macro is an operation defined with #define in preprocessor directive. It looked like a function, but performs a text substitution and does no data type checking. ex`; #define CIRCLE_AREA( r) (PI * (r) * (r)). Parenthesis are important
What is an #error token
error tokens
• Prints implementation-dependent message
• Tokens are groups of characters separated by
spaces
• #error 1 - Out of range error has 6 tokens
• Compilation may stop (depends on compiler)
What is a #pragma token?
pragma tokens
• Actions depend on compiler
• May use compiler-specific options
• Unrecognized #pragmas are ignored
What is the ## operator?
Tokenconcat - preprocessor macro that replaces any text in the form of a tuple in c or h file and makes one element. ex: #define CONCAT(a,b) a##b int main(void){ int xy = 30; print("%d", concat(x,y)); return 0; } - prints 30