Preprocessor

Ace your homework & exams now with Quizwiz!

Preprocessing expands macros in all lines that are not preprocessor directives (lines that do not have a # as the first non-white-space character) and in parts of some directives that are not skipped as part of a conditional compilation.

"Conditional compilation" directives allow you to suppress compilation of parts of a source file by testing a constant expression or identifier to determine which text blocks are passed on to the compiler and which text blocks are removed from the source file during preprocessing.

# include "filename" searching for the file typically begins where the source program was found

# include<filename> searching follows an implementation defined rule to find the file

In practical terms, there are two types of macros. "Object-like" macros take no arguments, whereas "function-like" macros can be defined to accept arguments so that they look and act like function calls. Because macros do not generate actual function calls, you can sometimes make programs run faster by replacing function calls with macros.

(In C++, inline functions are often a preferred method.) However, macros can create problems if you do not define and use them with care. You may have to use parentheses in macro definitions with arguments to preserve the proper precedence in an expression. Also, macros may not correctly handle expressions with side effects. See the getrandom example in The #define Directive for more information.

Once you have defined a macro, you cannot redefine it to a different value without first removing the original definition.

However, you can redefine the macro with exactly the same definition. Thus, the same definition can appear more than once in a program

The #define directive is typically used to associate meaningful identifiers with constants, keywords, and commonly used statements or expressions.

Identifiers that represent constants are sometimes called "symbolic constants" or "manifest constants." Identifiers that represent statements or expressions are called "macros." In this preprocessor documentation, only the term "macro" is used.

The macros in conio.h file is mostly used to provide console input and output. It is often used to create text-based user interface.

It also have some color functions to change color of text and background in a console application. CPUTS writes a string directly to the console. Also, CLRSCR clears the screen.

# include is the preferred way to tie the declarations together for a large program

It guarantees that all the source files will be supplied with the same definitions and variable declarations

Page 76

K&R

The stdio.h header file using prototype functions such as printf and scanf. The macros in stdio.h file procides many functions for file input and output devices.

Some of the macros in the stdio.h file gets, ungets or writes characters either from stream or from stdout like getwchar, putwchar, ungetwc. Some macros check errors like feof, ferror. Some opens files, closes, reads, seeks, write and flushs like fclose, fflush, fread, fseek, fwrite.

This ctype.h file has macros for all kinds of characters. This library declares several functions that are useful for testing and mapping characters.

Some of these macros checks if character is decimal digit, white-space using locale, alphabetic, lowercase, uppercase, punctuations, printable, wide character has graphical representation such as iswalpha, iswupper, iswdigit, iswspace, iswpunct, iswprint, iswgraph.

When the name of the macro is recognized in the program source text or in the arguments of certain other preprocessor commands, it is treated as a call to that macro.

The macro name is replaced by a copy of the macro body. If the macro accepts arguments, the actual arguments following the macro name are substituted for formal parameters in the macro body. The process of replacing a macro call with the processed copy of the body is called "expansion" of the macro call.

The C preprocessor or cpp is the macro preprocessor for the C and C++ computer programming languages.

The preprocessor provides the ability for the inclusion of header files, macro expansions, conditional compilation, and line control.

Macro substitution: # define name replacement text. It calls for a macro substitution of the simplest kind - subsequent occurrences of the token name will be replaced by the replacement text.

The scope of name defined with # define is from its point of definition to the end of source file being compiled. Substitution are made only for tokens and do not take place within a quoted strings.

Lines begin with # perhaps preceded by white space communicate with this preproceessor.

The syntax of of these lines is independent of the rest of the language, they may appear anywhere and have effect that lasts (independent of scope) until the end of the translation unit.

C provides certain language facilities by means of preprocessesor which a conceptionally separate first step in compilation

The two most frequently used feature are: # include to include the contents of a file during compilation. # define to replace a token by an arbitrary sequence of character

C provides certain language facilities by means of a preprocessor, which is conceptionally a separate first step in compilation.

The two most frequently used features are #include , to include the contents of a file during compilation, and #define, to replace a token by an arbitrary sequence of characters. Other features described in this section include conditional compilation and macros with arguments.

Preprocessor directives are lines included in the code of programs preceded by a hash sign (#). These lines are not program statements but directives for the preprocessor. The preprocessor examines the code before actual compilation of code begins and resolves all these directives before any code is actually generated by regular statements.

These preprocessor directives extend only across a single line of code. As soon as a newline character is found, the preprocessor directive is ends. No semicolon (;) is expected at the end of a preprocessor directive. The only way a preprocessor directive can extend through more than one line is by preceding the newline character at the end of the line by a backslash (\).

A preprocessor performs

macro substitution, conditional compilation, and inclusion of named files

extern declaration

to access the function prototype declarations for the library function from headers like <stdio.h>


Related study sets

Great Depression unit test study guide

View Set

Chapter 3: The Role of Evidence in Criminal Investigations

View Set

Ms. Robles-Suffix (end of words)---

View Set

Marketing Management Chapter Ten

View Set