Microcontroller Embedded C Programming Lecture 182| Conditional compilation directives

  • Post author:
  • Post category:Blog

 

Conditional compilation directives

 

Conditional compilation in ‘C’

There are various conditional compilation directives available in the ‘C’ programming language.  These are some of the conditional compilation directives, as shown below.

#if
#ifdef
#endif
#else
#undef
#ifndef

These directives help you to include or exclude individual code blocks based on various conditions set in the program. 

Conditional compilation directives in C
Figure 1. Conditional compilation in ‘C’

 

 

Now let’s explore how to use these conditional compilation directives.

 

#if and #endif directive

Figure 2. #if and #endif directive - Conditional compilation directives in C
Figure 2. #if and #endif directive

 

Syntax:

#if<constant expression>

#endif

You have to write #if followed by a constant expression. So, this must be a number or an integer. Like 0 or non-zero value.

 

Example

Here, #if 0, then you can write your code block, and the scope of the if is marked or decided by the following endif. That’s why the endif always should follow an if.

This directive checks whether the constant expression is a zero or non-zero value.

If the constant is 0, then the code block will not be included for the code compilation. 

If the constant is non zero, the code block will be included for the code compilation. 

endif directive marks the end of scope of #if, #ifdef, #ifndef, #else, #elif directives. 

 

Please don’t confuse this if with your decision-making if statement, both are entirely different. This is a conditional compilation directive, that ‘if’ is a ‘C’ statement. So, that ‘if’ what we use in decision-making is an if statement. So, that is checked during the runtime of the program. But this #if and #endif is checked during the pre-processor stage of the compilation, whether to include a code block or not in the final executable of the program.

In this case, this code block will not be part of the final executable of the program.

How to use  #if and #endif in a program?

#if 0
   float radius = 0;
   printf("This is circle area calculation program\n");
   fflush(stdout);
   printf("Enter the radius :");
   fflush(stdout);
   scanf("%f", &radius);
   printf("Area of circle = %f\n",(3.1415 * radius * radius));
   fflush(stdout);
#endif

   float base, height;
   printf("This is Triangle area calculation program\n");
   fflush(stdout);
   printf("Enter base and height: ");
   fflush(stdout);
   scanf("%f%f",&base, &height);
   printf("Area of triangle = %f\n", (0.5 * base * height));
   return 0;

Now here in this program, there are 2 code blocks. The first code block actually computes the area of a circle and the second code block computes the area of a triangle.

Conditional compilation directives in C
Figure 3. Example

In programs, you use comments to exclude the sentences. But comments are generally used to give some description or to provide some information. So, using that to remove a code block is not good.

Instead of that you can use pre-processor directive #if

Please note that you should remember 2 points:

  1. It must be a constant expression here.
  2. And this #if must be terminated by its associated #endif, otherwise you will face problems. 

 

Look at Figure 3, I can use #if 0. A #if must be terminated by its associated #endif. #endif actually decides the scope of this. So, now this code block is excluded from the build itself.

In the output, you can see that it directly goes to the second code block. It is not executing the first code block(Figure 3). 

 

 

#if …#else ……#endif

The #if, #else, and #endif are conditional compilation directives that are used in programming languages like C, C++, and C# to include or exclude parts of the code based on certain conditions.

Figure 4. #if …#else …#endif - Conditional compilation directives in C
Figure 4. #if …#else …#endif

 

Syntax:

You can also use #else in between #if and #endif, as shown in Figure 4. 

The #if directive tests whether a particular condition is true or false. And if it is true, the code between the #if and the corresponding #else or #endif directive is included in the compiled output. If the condition is false, the code between the #if and #else directives is skipped, and the code between #else and #endif directives (if present) is included instead.

The #else directive is optional and is used to specify an alternate code block that should be included in the compiled output when the condition tested by the preceding #if directive is false.

The #endif directive is used to mark the end of a conditional block and is required after every #if or #else directive.

 

Example

Here, a  #if constant expression is 1. So, code block-1 will be included for the build, and code block-2 will not be considered for the build.

Suppose, #if constant expression is 0, then code block-2 will be considered for the build. 

 

Let’s see how you use this in a program, as shown in below.

In the program, I use #if constant expression 1, after the code block-1 I will use #else, and then after the code block-2 I use #endif.  

In this case, the #if block will be included in the compiled output, and it prints an area calculation program.

So I use if 0, in that case, the second code block will be the output. 

That’s why use  #else when you want to execute either one of the code blocks.

#if 1
   float radius = 0;
   printf("This is circle area calculation program\n");
   fflush(stdout);
   printf("Enter the radius :");
   fflush(stdout);
   scanf("%f", &radius);
   printf("Area of circle = %f\n",(3.1415 * radius * radius));
   fflush(stdout);


#else
  float base, height;
  printf("This is Triangle area calculation program\n");
  fflush(stdout);
  printf("Enter base and height: ");
  fflush(stdout);
  scanf("%f%f",&base, &height);
  printf("Area of triangle = %f\n", (0.5 * base * height));
#endif

return 0;

 

 

#ifdef

Here as its name indicates so the evaluation of this pre-processor directive depends upon the definition of a macro. That’s why it is called #ifdef (that is if defined).

That’s the reason we use an identifier for the evaluation, so not a constant expression. 

Figure 6. #ifdef
Figure 5. #ifdef

 

#ifdef directive checks whether the identifier is defined in the program or not. If the identifier is defined, the code block will be included for compilation. 

 

Syntax: #ifdef, there is a space and you should use an identifier. And as usual, you have to use endif to terminate the scope. 

Example

If the NEW_FEATURE macro is defined in the program, then only include the code block, otherwise exclude the code block. That’s the meaning of those statements. 

 

 

#ifndef

The meaning of this code block is, if a new feature is not defined then it executes the code block. 

Figure 7. #ifndef
Figure 6. #ifndef

 

 

#ifdef and #else

Now you can also use #else along with  #ifdef.

Figure 8. #ifdef and #else
Figure 7. #ifdef and #else

If the NEW_FEATURE macro is really defined, then the code block of the new feature will be included. If the NEW_FEATURE macro is not defined, then the code block for the old feature will be included.

In the following article, let’s learn about the #if and ‘defined’ operators.

 

Get the Microcontroller Embedded C Programming Full Course Here.

FastBit Embedded Brain Academy Courses

Click here: https://fastbitlab.com/course1

 

FastBitLab

The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries.

Leave a Reply