C8: C Pre-Preprocessor Flashcards

1
Q

What can the pre-processor do?

A

It can

  1. include header files,
  2. include external files through use of directives,
  3. define symbolic constants and macros,
  4. do conditional compilation, and
  5. handle pre-processor directives.\
  6. process tokens - replace occurrences of text in file
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the two forms of preprocessor directives for including external files?

A
#include 
• For standard library header files
• Searches predesignated directories
#include "filename"
• Normally used for programmer-defined files
• Searches in current directory
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Name two ways to declare constants in C. Which way uses the preprocessor?

A
  1. const variables (ex: const int PI = 3.14;)
  2. 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a macro in C?

A

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);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is conditional compilation (preprocessor directive)?

A
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the use of a header guard in preprocessing?

A
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a token?

A

A token is an occurrence of text in a file. (not the strings in the code, .. the actual text of the code.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the # operator/

A
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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does the # operator do?

A
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the predefined symbolic constants in C?

A
\_\_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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a symbolic constant?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a macro?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is an #error token

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a #pragma token?

A

pragma tokens

• Actions depend on compiler
• May use compiler-specific options
• Unrecognized #pragmas are ignored

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the ## operator?

A
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the # operator/

A

macro that causes the corresponding actual argument to be enclosed in double quotations marks.