The C preprocessor Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Source Code:

A

The C program

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

C preprocessor:

A

Processes source code before passing it to compiler.

It creates Expanded Source Code (filename.I)

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

directives:

its 4 types u know?

A
C preprocessor Commands. Begin with #
Preferred to place them at top.
types:
1. Macro expansion.
2. File Inclusion.
3. Conditional Compilation.
4. Miscellaneous directives.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Build process:

A

Steps from writing C program to executing it.

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

Macro Expansion:

its rules and terminology.

A

define UPPER 25

macro definition: or just macro:

during preprocessing,when the preprocessor finds #define directive, it searches for macro templates in entire program, on finding one it replaces it with its macro expansion.
UPPER : Marco Templates. customary use capital letters.
25 : their corresponding macro expansion
template n expansion separated by space or tabs.

Never terminate macro definition by a semicolon.

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

Why use #define directive than its other alternatives.

variables at times

A
  1. readability.
  2. to just make the change once instead of finding every occurrence of constant and then changing it.
    Why prefer macro? cuz using vars is a bad idea for foll. 3 reasons:
    -Its inefficient, Compiler generates faster and compact code for constants than variables.
    - Encourages sloppy thinking, cuz using a variable for a constant.. it never changes.. hard to think of it as a variable.
    - Danger, a variable might inadvertently get altered somewhere.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Some uses of C #define directive:

A
  • to replace certain symbols by words.
  • to replace conditions.
  • to replace entire statement.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Macros with Arguments:

A

define area(x) (3.14xx)

x in macro template is argument tht matches x in macro expansion. exp will be substituted for all area(x).

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

Points to remember while writing macros with argument
or
Rules

A
  1. No blanks btw arg and macro template, otherwise arg will become a part of expansion.
  2. Enclose the macro expansions always by paranthesis. (precedence, results unexpected)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

is multiple line macro a thing?

A

using a back slash \ at the end it is possible.

#define HLINE for(i=0;i<79;i++ )\ 
                           printf( "%c",196);\
                            }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Debugging Macro

A

View expanded source code to check how macro is getting expanded.

At command prompt generate that file by:

cpp filename.c

filename.I will be now in C:\TC\BIN directory.

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

difference between Macros and Functions:

A

Preprocessors literally replace macro templates with macro expansion.
As against this, in a function call control is passed to a function along with arguments, some calculations are performed in the function and the useful value returned.

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

When to use function
when to use macro
why?

A

If Macro is short then use macro as it does take time to pass args and getting back returned value and hence function slows down the program. Macro would make a nice shorthand.

If macro is fairly large then replace it with the function. As macro makes program run faster but increase file size, function makes smaller and compact program.
Macro used 100 times, would go and expand 100 times increasing size, but func is used, then even if called from 100 diff places, it would take same amount of place in program.

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

File Inclusion:

A
#include "filename"  :
Looks for file in current directory and in specified list of directories as mentioned in the include search path, which can be set up (every compiler has its way).
#include  :
looks for the file only in the specified list of directories.

Command to include a file in another. Entire contents of filename are inserted in source code at that point in program.

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

when and why is this used?

file inclusion

A
  1. Modular program. When a program is large it is divided into groups of similar functions and all are included at the top using file inclusion in the main program.
  2. Some functions and macro definitions are frequently used so we store it in a file and that file is included in every program.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Conditional Compilation:

A

To have the compiler skip over part of a source code by inserting preprocessing commands #ifdef and #endif.

#ifdef macroname
  statement(s);
#endif

[ include #define macroname and it will proceed as usual]

17
Q

When would u like to compile only a part of your program:

A
  1. Clients usually want to undo the change they had asked, in that case to “comment out” the code we cant use comment as it might have comments and we cant have nested comments in c, so we use #ifdef.
  2. To make the program portable on diff machines.
  3. To avoid ‘Multiple declaration of a function’.
18
Q

How to make programs portable?

A

To make programs portable:

DOUBT LOOK INTO IT LATER

19
Q

conditional compilation directives:

A
  1. # ifdef MACRONAME… #else… #endif
  2. # ifndef MACRONAME .. #else….. #endif
3. #if Condtion ... #elif condition... #else .... #endif
#elif reduces number of #endif
Condition of MACRONAMES. if true (non zero) then corresponding block compiled.
20
Q

Miscellaneous Directives:

A
  1. # undef

2. #pragma

21
Q

undef directive:

A
#undef macro template
Undefines that macro template and brings changes in conditional compilation following this.
22
Q

pragma directives:

A
1. 
#pragma startup functionName
#pragma exit finctionName

These function cannot receive nor return any value.

declare those functions prototypes above,
startup executes at the beginning of the program before main and exit allow the respective function to execute before the program terminates.
If u want two functions to get executed at startup then their pragmas should be defined in reverse order in which u want them to get called.

  1. # pragma warnsuppress any warnings generated by compiler.
    2 most common:
    1. bad programming. not using void for non returning function.
    2. Writting code that might cause runtime error. Like assigning uninitialised pointer.
23
Q

pragma warn

A
#pragma warn -rvl   /* return vlaue */
#pragma warn -par /* parameter not used */
#pargma warn -rch /* unreachable code */

if - replaced by + then earnings are showed. THis is used when we want to just focus on errors and look at wearing later.

24
Q

The Build Process:

A

C Source Code [filname.C) goes through preprocessor and results in
Expanded SOURCE CODE (filname.i)
goes through compiler and gives
ASSEMBLY CODE (filname.ASM) goes through assembler and gives
RELOCATABLE OBJECT CODE
goes through linker along with object code of library functions and gives
EXECUTABLE CODE (filname.EXE)
this is sent to LOADER to execute.

25
Q

Preprocessing:

A

based on preprocessor directives, the expanded source code is stored in an intermediate file,

26
Q

Compilation:

A

pragma can control warnings, if error free code then compiler translates the expanded source code in C into an equivalent assembly language program. diff processors (CPU) support diff set of assembly instructions.

27
Q

Assembling:

A

Gives relocatable object code.
,obj file is specially formatted binary file that contains set of machine language instructions and data resulting from the language translation process.

28
Q

Why cant .obj directly get executed?

what does object contains?h

A

The object file contains a header and several sections. The header describes the sections that follow it, sections are:
1. Text Section : contains code blocks.
2. Data Section : global var & initial values.
3. Bss ( block started by symbol): uninitialised global variables.
here relocatable means program has no specific memory addresses assigned to code and data section, all address are relative offsets.

The object code also contain ‘Symbol table’ which contains names and locations of all global vars and functions referenced within source file.
However not all functions are always defined in same file hence part of table may be incomplete.
these are symbols that refer to var n funds defined in other source files. linker resolves this

29
Q

Linking:

A

for multiple source files , multiple obj files get created.
Linker combines all object files and in that process resolves all unresolved symbols.
merges all text, data section, BSS sections of object file.
In executable file, all input object files will be in text section.
Data section : Initialised vars
BSS: uninitialised vars.

30
Q

What might stop linking process:

A

misspelling the name of library
using incorrect number or type of parameters for a function.
then it doesn’t create binary executable file.

31
Q

what does loader do:

A

loads EXE file, loader in memory.

And after calling some platform specific intialisation functions main() would be called.

32
Q

object file for windows, linux:

A

Windows: Portable Executable (PE) file format.
linux:
COFF : Common Object file format.
ELF : Executable and linking format.