Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Syntax 
3.
Example 1
3.1.
CODE
3.2.
OUTPUT
4.
Example 2
4.1.
CODE
4.2.
OUTPUT
5.
Frequently Asked Questions
5.1.
What is the #if directive?
5.2.
What does the preprocessor evaluate?
5.3.
How does the #if directive is closed?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

C #if

Author Ayushi Poddar
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

The C preprocessor is a microprocessor used by the compiler to convert your code before compilation. It allows us to add macros; hence is called micro preprocessors. These directives are executed before compilation and start with a hash symbol. There are a lot of preprocessor directives like:

  • #include
  • #define
  • #undef
  • #ifdef
  • #ifndef
  • #if
  • #else
  • #elif
  • #endif
  • #error
  • #pragma 
     

The #if is a preprocessor directive that allows for conditional compilation in the C programming language. The #if preprocessor directive basically evaluates the condition or the expression. The preprocessor evaluates the expression given with if directive to check if the subsequent code will be included in the compilation process or not. The #if directive must always be closed be with an #endif directive.

Also see: C Static Function, and Tribonacci Series

Syntax 

#if expression  
//program code  
#endif 
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Example 1

CODE

#include <conio.h>  
#include <stdio.h>  

#define NUM 0  
void main() {  
#if (NUM==0)  
printf("Value of Num is : %d",NUM);  
#endif         
getch();  
} 

OUTPUT

Value of Num is : 0

Example 2

CODE

#include <stdio.h>
#define LEARNING 1
int main()
{
   printf("Coding Ninjas Studio is a great ");
   #if LEARNING
   printf("learning ");
   #endif
   printf("platform.\n");
   return 0;
}

OUTPUT

Coding Ninjas Studio is a great learning platform.

 

You can also read about dynamic array in c and Short int in C Programming

Frequently Asked Questions

What is the #if directive?

The #if is a preprocessor directive that allows for conditional compilation in the C programming language. The #if preprocessor directive basically evaluates the condition or the expression.

What does the preprocessor evaluate?

The preprocessor evaluates the expression given with if directive to check if the subsequent code will be included in the compilation process or not. 

How does the #if directive is closed?

The #if directive must always be closed be with an #endif directive.

Conclusion

In this article, we have extensively about the C #if directive. We hope that this blog has helped you enhance your knowledge regarding #if directive in C language, and if you want to learn more, check out our articles here. Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But suppose you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problemsinterview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blog to help other ninjas grow. Happy Coding!

 

 

Previous article
C #ifndef
Next article
C #else
Live masterclass