Thursday, July 12, 2018

C #if

Description

In the C Programming Language, the #if directive allows for conditional compilation. The preprocessor evaluates an expression provided with the #if directive to determine if the subsequent code should be included in the compilation process.

The #if preprocessor directive evaluates the expression or condition. If condition is true, it executes the code otherwise #elseif or #else or #endif code is executed.

Syntax:





#if expression
//code
#endif



Syntax with #else:



#if expression
//if code
#else
//else code
#endif



Syntax with #elif and #else:



#if expression
//if code
#elif expression
//elif code
#else
//else code
#endif



Note

  • The #if directive must be closed by an #endif directive.

C #if example

Let's see a simple example to use #if preprocessor directive.





#include <stdio.h>
#include <conio.h>
#define NUMBER 0
void main() {
#if (NUMBER==0)
printf("Value of Number is: %d",NUMBER);
#endif
getch();
}



Output:

Value of Number is: 0

Let's see another example to understand the #if directive clearly.





#include <stdio.h>
#include <conio.h>
#define NUMBER 1
void main() {
clrscr();
#if (NUMBER==0)
printf("1 Value of Number is: %d",NUMBER);
#endif

#if (NUMBER==1)
printf("2 Value of Number is: %d",NUMBER);
#endif
getch();
}



Output:

2 Value of Number is: 1

Example

The following example shows how to use the #if directive in the C language:





/* Example using #if directive */

#include <stdio.h>

#define WINDOWS 1

int main()
{
printf("C programming is a great ");

#if WINDOWS
printf("Windows ");
#endif

printf("resource.\n");

return 0;
}



Here is the output of the executable program:

C programming is a great Windows resource.




Instagram



C #ifndef

Description

In the C Programming Language, the #ifndef directive allows for conditional compilation. The preprocessor determines if the provided macro does not exist before including the subsequent code in the compilation process.

The #ifndef preprocessor directive checks if macro is not defined by #define. If yes, it executes the code otherwise #else code is executed, if present.

Syntax:






#ifndef MACRO
//code
#endif



Syntax with #else:



#ifndef MACRO
//successful code
#else
//else code
#endif



Note

The #ifndef directive must be closed by an #endif directive.

C #ifndef example

Let's see a simple example to use #ifndef preprocessor directive.





#include <stdio.h>
#include <conio.h>
#define INPUT
void main() {
int a=0;
#ifndef INPUT
a=2;
#else
printf("Enter a:");
scanf("%d", &a);
#endif
printf("Value of a: %d\n", a);
getch();
}



Output:

Enter a:5

Value of a: 5

But, if you don't define INPUT, it will execute the code of #ifndef.





#include <stdio.h>
#include <conio.h>
void main() {
int a=0;
#ifndef INPUT
a=2;
#else
printf("Enter a:");
scanf("%d", &a);
#endif
printf("Value of a: %d\n", a);
getch();
}



Output:

Value of a: 2

Example

The following example shows how to use the #ifndef directive in the C language:





/* Example using #ifndef directive by C programming */

#include <stdio.h>

#define YEARS_OLD 15
#ifndef YEARS_OLD
#define YEARS_OLD 10
#endif

int main()
{
printf("C programming is over %d years old.\n", YEARS_OLD);

return 0;
}



In this example, if the macro YEARS_OLD is not defined before the #ifndef directive is encountered, it will be defined with a value of 10.

Here is the output of the executable program:

C programming is over 15 years old.

If you remove the line #define YEARS_OLD 15, you will see the following output from the executable program:

C programming is over 10 years old.




Instagram



C #ifdef

Description

In the C Programming Language, the #ifdef directive allows for conditional compilation. The preprocessor determines if the provided macro exists before including the subsequent code in the compilation process.

The #ifdef preprocessor directive checks if macro is defined by #define. If yes, it executes the code otherwise #else code is executed, if present.

Syntax:





#ifdef MACRO
//code
#endif



Syntax with #else:





#ifdef MACRO
//successful code
#else
//else code
#endif



C #ifdef example

Let's see a simple example to use #ifdef preprocessor directive.





#include <stdio.h>
#include <conio.h>
#define NOINPUT
void main() {
int a=0;
#ifdef NOINPUT
a=2;
#else
printf("Enter a:");
scanf("%d", &a);
#endif
printf("Value of a: %d\n", a);
getch();
}



Output:

Value of a: 2

But, if you don't define NOINPUT, it will ask user to enter a number.





#include <stdio.h>
#include <conio.h>
void main() {
int a=0;
#ifdef NOINPUT
a=2;
#else
printf("Enter a:");
scanf("%d", &a);
#endif

printf("Value of a: %d\n", a);
getch();
}



Output:

Enter a:5

Value of a: 5

Example

The following example shows how to use the #ifdef directive in the C language:





/* Example using #ifdef directive by C programming*/

#include <stdio.h>

#define YEARS_OLD 10

int main()
{
#ifdef YEARS_OLD
printf("C programming is over %d years old.\n", YEARS_OLD);
#endif

printf("C programming is a great resource.\n");

return 0;
}



Here is the output of the executable program:

C programming is over 10 years old.

C programming is a great resource.

A common use for the #ifdef directive is to enable the insertion of platform specific source code into a program.

The following is an example of this:





/* Example using #ifdef directive for inserting platform
* specific source code by C programming*/

#include <stdio.h>

#define UNIX 1

int main()
{
#ifdef UNIX
printf("UNIX specific function calls go here.\n");
#endif

printf("C programming is over 10 years old.\n");

return 0;
}



The output of this program is:

UNIX specific function calls go here.

C programming is over 10 years old.

In this example, the UNIX source code is enabled. To disable the UNIX source code, change the line #define UNIX 1 to #define UNIX 0.




Instagram



C #undef

The #undef preprocessor directive is used to undefine the constant or macro defined by #define.

Description

In the C Programming Language, the #undef directive tells the preprocessor to remove all definitions for the specified macro. A macro can be redefined after it has been removed by the #undef directive.

Once a macro is undefined, an #ifdef directive on that macro will evaluate to false.

Syntax:





#undef token



Let's see a simple example to define and undefine a constant.





#include <stdio.h>
#define PI 3.14
#undef PI
main() {
printf("%f",PI);
}



Output:





Compile Time Error: 'PI' undeclared



The #undef directive is used to define the preprocessor constant to a limited scope so that you can declare constant again.

Let's see an example where we are defining and undefining number variable. But before being undefined, it was used by square variable.





#include
#define number 5
int square=number*number;
#undef number
main() {
printf("%d",square);
}



Output:

25

Example

The following example shows how to use the #undef directive:





/* Example using #undef directive by C programming */

#include <stdio.h>

#define YEARS_OLD 10

#undef YEARS_OLD

int main()
{
#ifdef YEARS_OLD
printf("c programming is over %d years old.\n", YEARS_OLD);
#endif

printf("c programming is a great resource.\n");

return 0;
}



In this example, the YEARS_OLD macro is first defined with a value of 10 and then undefined using the #undef directive. Since the macro no longer exists, the statement #ifdef YEARS_OLD evaluates to false. This causes the subsequent printf function to be skipped.

Here is the output of the executable program:





c programming is a great resource.






Instagram