The file add1.c contains the function definition as follows.
void add()
{
int a,b,c;
printf(“enter two numbers”);
scanf(%d%d”,&a,&b);
c=a+b;
printf(“c value is:%d”,c);
}
3 : CONDITIONAL COMPILATION DIRECTIVES : C preprocessor also supports number
of conditional compilation directives as
1 : #undef : Undefined a macro
2 : #ifdef : Tests for a macro definition.
3 : #endif : Specifies the end of #if.
4 : #if : Tests compile-time condition.
5 : #else : Specifies alternative when #if test fails.
These are used to select a particular segment of code for compilation depending on the
condition.
C PROGRAMMING Page 191
,EXAMPLE PROGRAM FOR CONDITIONAL COMPILATION DIRECTIVES:
A) EXAMPLE PROGRAM FOR #IFDEF, #ELSE AND #ENDIF IN C:
“#ifdef” directive checks whether particular macro is defined or not. If it is defined, “If”
clause statements are included in source file.
Otherwise, “else” clause statements are included in source file for compilation and
execution.
Ex:
#include <stdio.h>
#define RAJU 100
int main()
{
#ifdef RAJU
printf("RAJU is defined. So, this line will be added in " \
"this C file\n");
#else
printf("RAJU is not defined\n");
#endif
return 0;
}
OUTPUT:
RAJU is defined. So, this line will be added in this C file
B) EXAMPLE PROGRAM FOR #IFNDEF AND #ENDIF IN C:
#ifndef exactly acts as reverse as #ifdef directive. If particular macro is not defined, “If”
clause statements are included in source file.
Otherwise, else clause statements are included in source file for compilation and
execution.
C PROGRAMMING Page 192
, Ex:
#include <stdio.h>
#define RAJU 100
int main()
{
#ifndef SELVA
{
printf("SELVA is not defined. So, now we are going to " \
"define here\n");
#define SELVA 300
}
#else
printf("SELVA is already defined in the program”);
#endif
return 0;
}
OUTPUT:
SELVA is not defined. So, now we are going to define here
C) EXAMPLE PROGRAM FOR #IF, #ELSE AND #ENDIF IN C:
“If” clause statement is included in source file if given condition is true.
Otherwise, else clause statement is included in source file for compilation and execution.
Ex:
#include <stdio.h>
#define a 100
int main()
{
#if (a==100)
printf("This line will be added in this C file since " \
"a \= 100\n");
#else
printf("This line will be added in this C file since " \
"a is not equal to 100\n");
#endif
return 0;
}
OUTPUT:
This line will be added in this C file since a = 100
C PROGRAMMING Page 193
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller akhilagouri. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for £15.79. You're not tied to anything after your purchase.