answersLogoWhite

0

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

8y ago

Still curious? Ask our experts.

Chat with our AI personalities

FranFran
I've made my fair share of mistakes, and if I can help you avoid a few, I'd sure like to try.
Chat with Fran
ProfessorProfessor
I will give you the most educated answer.
Chat with Professor
RafaRafa
There's no fun in playing it safe. Why not try something a little unhinged?
Chat with Rafa

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.