answersLogoWhite

0

What are compiler directives?

Updated: 8/11/2023
User Avatar

Wiki User

6y ago

Best Answer

Compiler directives instruct the preprocessor to perform certain tasks prior to compiling the source code. The language compiler can only compile C code, but macros (lines beginning with a # symbol) are not part of the C language, so these need to be processed before the compiler can do its job.

The most common compiler directive is the #include directive, which instructs the preprocessor to import the named file, effectively copy/pasting the content of that file in place of the directive. The content may itself contain directives and these must also be processed accordingly before the content is inserted.

Note that the insertion takes place in a temporary file; the source file is not changed by the preprocessor. The temporary file is usually deleted after compilation is complete, however you can configure the compiler to retain these files so you can see what code is produced by the preprocessor and thus what the compiler "sees".

The #define and #undefine compiler directives define or undefine macros; symbols which the preprocessor can refer to while processing other directives. Macros can also be defined via the command line.

The #ifdef, #ifndef, #elif, #else and #endif compiler directives are used in conditional compilation in conjunction with macro definitions. These tell the preprocessor which parts of the source code to include in or exclude from the temporary file. This is useful when writing debug code that we wish to include in a debug build but exclude from a retail build, or we wish to cater for different architectures and require different libraries and function calls in order to cater for them.

A macro symbol can also be given a value. Wherever that symbol appears in the code, the symbol is replaced with the value. This provides a convenient text-replacement system that can help make code easier to read. However, a macro can also be used to define a function (a macro function). These differ from ordinary functions in that they are always inline expanded, however they are not type-safe and the compiler cannot help you debug them (the compiler never sees the macro -- it only sees the inline expanded code produced by the macro). Macro functions are best avoided, however when used with care they can help to resolve complex problems that cannot easily be resolved within the language itself. It's best to think of macro functions as being code generators but, because they are not part of the language, its best to keep their usage as simple as possible.

As well as processing compiler directives, the preprocessor also strips out all the user-comments from the source file. Even if a compiler could read a comment it certainly wouldn't understand it, so to keep the compiler implementation as simple and as efficient as possible, all non-C code is completely stripped out. Even the redundant whitespace and newline characters are stripped out. The end result is a compact source file which can be easily compiled -- assuming no errors have been generated by your macros!

User Avatar

Wiki User

6y ago
This answer is:
User Avatar

Add your answer:

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

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.


What is the use of 'pragma' in C language?

The #pragma directive is a compiler specific instruction. There are many things you can tell the compiler. For instance, the #pragma pack n directive tells the compiler to override the /Zpn command line argument and to use a new default structure packing value. To see all of the possible #pragma directives, go to online help, and index by #pragma directives, C/C++. You probably only need to type in #pra and then click on C/C++ to get this far.


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 is the difference between define and undefine?

# define and # undef are compiler directives in C and C++. The # define directive creates a definition for something that will be replaced multiple times in the code. For example: # define HELLO 5 Creates an association between HELLO and replaces it with 5 in the code (for the compiler only). The # undef (undefine) counterpart removes the definition from what the compiler sees. It is usually specified when either the definition should no longer be used or when the definition needs to change.


How many types of compiler naming in list?

cross compiler . hybird compiler . post compiler. ideal compiler. intelligence compiler.

Related questions

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.


What is the use of 'pragma' in C language?

The #pragma directive is a compiler specific instruction. There are many things you can tell the compiler. For instance, the #pragma pack n directive tells the compiler to override the /Zpn command line argument and to use a new default structure packing value. To see all of the possible #pragma directives, go to online help, and index by #pragma directives, C/C++. You probably only need to type in #pra and then click on C/C++ to get this far.


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 does advance directives mean?

what does advance directives mean


What is difference between what and define?

# define and # undef are compiler directives in C and C++. The # define directive creates a definition for something that will be replaced multiple times in the code. For example: # define HELLO 5 Creates an association between HELLO and replaces it with 5 in the code (for the compiler only). The # undef (undefine) counterpart removes the definition from what the compiler sees. It is usually specified when either the definition should no longer be used or when the definition needs to change.


What is the difference between define and undefine?

# define and # undef are compiler directives in C and C++. The # define directive creates a definition for something that will be replaced multiple times in the code. For example: # define HELLO 5 Creates an association between HELLO and replaces it with 5 in the code (for the compiler only). The # undef (undefine) counterpart removes the definition from what the compiler sees. It is usually specified when either the definition should no longer be used or when the definition needs to change.


How many types of compiler naming in list?

cross compiler . hybird compiler . post compiler. ideal compiler. intelligence compiler.


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.


Are directives enforceable in the county court?

Insufficient information is included with which to answer. Directives from WHO, and/or in WHAT form?


What is DML Compiler?

What is difference between Compiler and DML Compiler


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 the difference between a compiler design and a compiler?

In Compiler there is no design word ,whereas in compiler design there is design word