"Mastering" #if-#else conditional compilation directives
#include <stdio.h>#defineScore 99intMainintargcConst Char*argv[]) { //Traditional Way//int score =;// //determine which grade the score belongs to//if (score<60) {//printf ("e\n");//}else if (score<=69) {//printf ("d\n");//}else if (score<=79) {//printf ("c\n");//}else if (score<=89) {//printf ("b\n");//}else{//printf ("a\n");// } #ifScore < 60printf ("e\n");#elifScore <= 69printf ("d\n");#elifScore <= 79printf ("c\n");#elifScore <= 89printf ("b\n");#elseprintf ("a\n");#endif return 0;}
Summary: Comments are not compiled, conditional compilation is partially compiled
"Mastering" #ifdef conditional compilation Instructions
conditional Compilation Directives 1 #if #elif #else #endif 2) #ifdef
#include <stdio.h>#defineDEBUG1 1#defineDEBUG2 0intMainintargcConst Char*argv[]) { intA =0; //ifdef detect if a macro is defined#ifdef DEBUG//The DEBUG system already defines the macro.A =Ten; #else a=10000; #endifprintf ("%d\n", a); //Ifndef detect if macros are defined ifndef if not defined#ifndef DEBUG2 a= -; #elsea= -1; #endifprintf ("%d\n", a); return 0;}
Ten-1
<29> "Mastering" #if-#else conditional compilation Instructions +