answersLogoWhite

0


Best Answer

For example:

define a symbol
undefine a symbol
include another file
conditionally exclude/include parts
etc


As the name suggests, a preprocessor directive modifies the source code before the compiler sees it. This way the developer can keep the logic at a high level while the compiler does all of the work. As an example:

# define N 5

if (TestingValue == N)

is cleaner code and easier to change should the value for N change. It leads to better modular code for code maintainers.

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What are preprocessor directives or statements?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

In C plus plus what sign do all preprocessor directives start with?

all preprocessor directives start with #(hash) symbol in both c & c++


What is the difference between preprocessor directives and header files?

Preprocessor: All the preprocessor commands written in a high level language are processed by the preprocessor before compiler takes over.Example: "#define MAX_ROWS 10"Preprocessor finds all the places and replaces MAX_ROWS with 10 in the files of the project.Compiler: This software, converts the code written in high-level language into object file. Compiler converts all the files of a given project at once.


What are preprocessor directives in c programing language?

Preprocessor directives are instructions to the preprocessor which modify the source code prior to compilation. The compiler never sees the directives, it only sees the modified source code. Preprocessor directives can be used to insert the contents of one file into another (#include), define or undefine a macro (#define, #undef), provide conditional compilation (#if, #ifdef, #ifndef, #else, #endif) or provide some implementation-defined operation (#pragma). When the preprocessor acts upon a directive, the directive is not included in the modified file. Where a directive defines a macro, all occurrences of the macro name within the source code are expanded according to the definition. Given that the compiler never sees that definition, this can lead to some obscure error messages where macro expansion results in a compile-time error.


What are the functions of a c processor?

The preprocessor handles directives for source file inclusion (#include), macro definitions (#define), and conditional inclusion (#if).


When do preprocessor directives execute in c plus plus?

Preprocessing is the first stage of compilation, where macros are expanded, conditional compilation established and code replaced according to the specified directives. The resulting code produces intermediate source files which are then compiled by the main compilation process. Your IDE may include options to retain these intermediate files so you may examine them.

Related questions

Why all preprocessor directives starts with hashmark?

Because otherwise the preprocessor would ignore them.


In C plus plus what sign do all preprocessor directives start with?

all preprocessor directives start with #(hash) symbol in both c & c++


What is directives in c?

It's either the preprocessor instructions (#include #define etc), or the pragmas.


What is the difference between preprocessor directives and header files?

Preprocessor: All the preprocessor commands written in a high level language are processed by the preprocessor before compiler takes over.Example: "#define MAX_ROWS 10"Preprocessor finds all the places and replaces MAX_ROWS with 10 in the files of the project.Compiler: This software, converts the code written in high-level language into object file. Compiler converts all the files of a given project at once.


What are preprocessor directives in c programing language?

Preprocessor directives are instructions to the preprocessor which modify the source code prior to compilation. The compiler never sees the directives, it only sees the modified source code. Preprocessor directives can be used to insert the contents of one file into another (#include), define or undefine a macro (#define, #undef), provide conditional compilation (#if, #ifdef, #ifndef, #else, #endif) or provide some implementation-defined operation (#pragma). When the preprocessor acts upon a directive, the directive is not included in the modified file. Where a directive defines a macro, all occurrences of the macro name within the source code are expanded according to the definition. Given that the compiler never sees that definition, this can lead to some obscure error messages where macro expansion results in a compile-time error.


What are the function of a processor?

The preprocessor handles directives for source file inclusion (#include), macro definitions (#define), and conditional inclusion (#if).


What are the various preprocessor directives available in c?

Some of them are: #include #define #if, #else, endif, #elif, #ifdef, #ifndef #line #pragma


What are the functions of a c processor?

The preprocessor handles directives for source file inclusion (#include), macro definitions (#define), and conditional inclusion (#if).


When do preprocessor directives execute in c plus plus?

Preprocessing is the first stage of compilation, where macros are expanded, conditional compilation established and code replaced according to the specified directives. The resulting code produces intermediate source files which are then compiled by the main compilation process. Your IDE may include options to retain these intermediate files so you may examine them.


What step that converts source file directives to source code program statements.?

A pre-processor will scan your code files for directives and then evaluate the conditions specified by the directive. Then depending on the evaluation of the condition it may include certain program statements and/or ignore others before handing the "modified" source code to the compiler.


How does c plus plus code compiled?

When you invoke the compiler, the preprocessor (also known as the precompiler) runs first to process all the precompiler directives and macros; the lines beginning with the pound symbol (#) such as #define and #include. The preprocessor also strips out all of the comments. The preprocessor achieves this by creating one or more temporary, intermediate files containing nothing but C++ code. The compiler then compiles these intermediate files into object code which the linker uses to create the final executable.


What is difference between using declarative method and using directive method?

The simple answer is that directives are symbols that instruct or direct the preprocessor, while declarations are C++ symbols that are processed by the C++ compiler. The preprocessor and the compiler are essentially two completely separate operations. The preprocessor is tasked with modifying your source files in order to produce temporary intermediate source files which the compiler can process more efficiently. These intermediate files contain nothing but C++ code, thus the preprocessor's primary job is to strip out all the comments from your source files. Preprocessor directives are used to instruct the preprocessor in all other modifications, such as where and when to include one file within another and which sections of code are valid for a particular build. This is achieved by #define directives, in conjunction with #ifdef, #else and #endif directives. The #define directive defines a symbol. As the preprocessor encounters these directives, the symbols are placed in a definition table, which includes all the symbols that were initially passed via the command line. Some symbols have no value assigned to them; their presence in the table is sufficient to know they are defined, and the preprocessor can simply act according to whether a symbol is currently defined or not. The #undef directive is the antidote to #define, allowing symbols to be undefined (removed from the table) by the preprocessor. However, #define can also be used to assign a value to a symbol. This value acts a text replacement for the symbol, thus wherever the symbol is encountered within your code (outwith any other directives), the symbol is replaced with the text value associated with that symbol. These are known as macros. #define can also be used to declare macro functions, where the text replacement includes one or more arguments within parenthesis, followed by C++ code that processes those arguments. Thus all occurrences of the symbol in your code must include an argument list with the same number of arguments as the macro directive. The preprocessor then inline expands the macro function, replacing its arguments with the formal arguments provided by your code. The problem with #define in general is that it is not a C++ statement, it is a macro, and macros are not subject to the strong-typing that is fundamental to C++. So while you can use #define to declare literal constants, those constants are untyped. Moreover, since the compiler never sees the macro definition (it is stripped from the intermediate file), any errors that occur as a result of a macro expansion cannot be sourced back to the macro, which makes debugging difficult at best. In the case of macro functions, the debugger cannot help you at all, you're completely on your own. Thus when you have the option to use directives or declarations, always go with a declaration. C++ code is type safe, can be easily debugged, is easier to maintain, and usually results in much smaller compiled code. Only use the preprocessor to generate code that would be difficult or impossible to generate with the compiler alone. In other words, use directives specifically to direct the preprocessor. Never rely on them to generate C++ code unless you have no other option, or it would greatly simplify a far more complex C++ implementation.