answersLogoWhite

0


Best Answer

Conditional compilation is achieve through preprocessor directives. First, define the preprocessor symbols upon which conditional compilation depends, then test them using #if and #else preprocessor directives. A #endif directive indicates the end of the nearest enclosing conditional block, thus conditional blocks may be nested.

The following example demonstrates how we can conditionally define debug or production code based upon the absence or existence of the NDEBUG symbol:

#ifdef NDEBUG

/* all C code within this block is compiled when NDEBUG is defined (production code) */

#else

/* all C code within this block is compiled when NDEBUG is not defined (debug code) */

#endif

Note that the NDEBUG symbol is typically defined via the command line, however symbols can also be defined or undefined via the source using the #define and #undefine directives. For instance, header files typically require guards to protect against being included more than once in a compilation and preprocessor directives provide the conventional means of ensuring that is the case:

// myheader.h

#ifndef _MYHEADER_H_

#define _MYHEADER_H_

// all header code goes here...

#endif

By convention, preprocessing symbols (macros) are defined with all uppercase and are intentionally ugly to avoid any confusion with C identifiers. Header guards must be unique to each header thus they are typically based upon the header file name itself.

User Avatar

Wiki User

7y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you implement c program for conditional compilation?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is platform dependency of c plus plus?

C++ has no platform dependency. If a compiler exists for a platform (and few don't) code can be written for that platform. Where platforms have different methods to do the same thing, conditional compilation can be used to cater for those differences, thus the same source code can be compiled on any platform simply by changing the definitions used by the conditional compilation directives. For instance, a program that caters for Unix and Windows platforms might contain the following conditional compilation: #ifdef __unix__ #include <unistd.h> #elif defined _WIN32 #include <windows.h> #endif The definition of __unix__ and _WIN32 must be mutually exclusive.


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.


C program to implement arithmetic assignment operator?

y=2x2+3x+1


Missing semicolon in c program is a compile time error?

It's a syntax error, which is detected during compilation, yes.


Write a program in C language to implement the apriori algorithm?

JavaScript is one program that has been written in C to implement the Apriori algorithm. There are also several other known programs available on the Internet that implement it as well.

Related questions

Where does the compilation of c program starts?

After main()


Why is c plus plus language more portable than c language?

They are equally portable. Conditional compilation is supported by both languages.


What is platform dependency of c plus plus?

C++ has no platform dependency. If a compiler exists for a platform (and few don't) code can be written for that platform. Where platforms have different methods to do the same thing, conditional compilation can be used to cater for those differences, thus the same source code can be compiled on any platform simply by changing the definitions used by the conditional compilation directives. For instance, a program that caters for Unix and Windows platforms might contain the following conditional compilation: #ifdef __unix__ #include <unistd.h> #elif defined _WIN32 #include <windows.h> #endif The definition of __unix__ and _WIN32 must be mutually exclusive.


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 is the sequnce to writing tha c program?

Writing the source(s).Compilation and linkage.Execution.


Write a program of binary heap in c or c language?

to implement operations on binary heap in c


Write a program in c language to implement framing methods like character stuffing?

A program in c language to implement framing methods like character stuffing can be grave sizeCRC-32 and the variable c50.


How do you run graphics program in C?

pro c language to implement linear search using pointers


How can you download c compiler?

write a c program to fine largest/smallest of 3no (using ?:ternary operator/conditional operator)


C program to implement arithmetic assignment operator?

y=2x2+3x+1


How to do a password using vhdl?

A VHDL program can be written to provide a password in the following ways.Using FPGA:Your program can be dumped into a FPGA. This works as per the program written. As the code is burned into it, the user can not see the code. Now, within your program you can have a conditional statement which serves as your password. For example,if (c=ramarav) then...................................................................Using CPLD:It also works in the similar manner.Using a GUI:We can implement a graphical user interface, but the easier way is to take the help of MATLAB tool. We can implement our VHDL code in MATLAB tool and it serves the purpose.


Missing semicolon in c program is a compile time error?

It's a syntax error, which is detected during compilation, yes.