C Sharpifdef - C Programming Tutorial
C Course / Preprocessor / C Sharpifdef

C Sharpifdef

BLUF: Understanding C Sharpifdef is a foundational part of learning C programming. This tutorial explains the core principles and syntax needed to implement this concept effectively.
Core Programming Principle: C Sharpifdef

C provides direct access to memory and system resources. Learn how C Sharpifdef leverages this power in the lesson below.

C #ifdef

Example

#ifdef MACRO

//code

#endif

Syntax with #else:

Example

#ifdef MACRO

//successful code

#else

//else code

#endif

C #ifdef example

Let's explore a basic illustration demonstrating the utilization of the #ifdef preprocessor directive.

Example

Example

#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:

Output

Value of a: 2

However, failing to specify NOINPUT will result in a prompt for the user to input a numerical value.

Example

Example

#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:

Output

Enter a:5

Value of a: 5

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience