Follow these steps to perform conditional compilation with preprocessor directives:
- Define a Qty macro and assign it an initial value:
#define Qty 10
- The user will be prompted to enter the price of a book:
printf("Enter price of a book ");
scanf("%f", &price);
- The total number of books is computed using the Qty*price formula:
totalAmount=Qty*price;
- On the basis of the Qty macro, the #if, #elif, #else, and #endif directives are used to determine the discount on the total number.
- Once the discount percentage is determined, the amount after deducting the discount is computed and is assigned to the afterDisc variable:
afterDisc=totalAmount - (totalAmount*discount)/100;
- The festival discount is also computed on the basis of the FestivalOffer macro. That is, the #ifdef, #else, and #endif directives are used to confirm whether the FestivalOffer macro is defined and, accordingly, the amount that the customer has to pay after deducting the festival discount is computed:
#ifdef FestivalOffer
afterFDisc=afterDisc-(totalAmount*FestivalOffer)/100;
#else
afterFDisc=afterDisc;
#endif
- The #if defined directive is used to confirm whether the DiscountCoupon macro is defined in the program or not. And, accordingly, the user is informed whether they are eligible for the discount coupon:
#if defined (DiscountCoupon)
printf("You are also eligible for a discount coupon of $ %d\n", DiscountCoupon);
#endif
- The preprocessor directives, #ifndef and #endif, are used to determine whether the Kindle macro is defined or not. If the Kindle macro is not yet defined, it is defined and its value is set. Accordingly, the user is informed of how many months they will be eligible for the Kindle version of the book:
#ifndef Kindle
#define Kindle 1
#endif
printf("You can use the Kindle version of the book for %d
month(s)\n", Kindle);
The program for performing conditional compilation with preprocessor directives is shown in the following code snippet:
// condcompile.c
#include <stdio.h>
#define Qty 10
#define FestivalOffer 2
#define DiscountCoupon 5
#define Kindle 2
int main()
{
int discount;
float price, totalAmount, afterDisc, afterFDisc;
printf("Enter price of a book ");
scanf("%f", &price);
#if Qty >= 10
discount=15;
#elif Qty >=5
discount=10;
#else
discount=5;
#endif
totalAmount=Qty*price;
afterDisc=totalAmount - (totalAmount*discount)/100;
#ifdef FestivalOffer
afterFDisc=afterDisc-(totalAmount*FestivalOffer)/100;
#else
afterFDisc=afterDisc;
#endif
printf("Quantity = %d, Price is $ %.2f, total amount for the
books is $ %.2f\n", Qty, price, totalAmount);
printf("Discount is %d%% and the total amount after
discount is $ %.2f\n", discount, afterDisc);
#ifdef FestivalOffer
printf("Festival discount is %d%%, the total amount
after festival discount is $ %.2f\n",
FestivalOffer, afterFDisc);
#endif
#if defined (DiscountCoupon)
printf("You are also eligible for a discount
coupon of $ %d\n", DiscountCoupon);
#endif
#ifndef Kindle
#define Kindle 1
#endif
printf("You can use the Kindle version of the book
for %d month(s)\n", Kindle);
return 0;
}
Now, let's go behind the scenes to understand the code better.