La lecture en ligne est gratuite
Le téléchargement nécessite un accès à la bibliothèque YouScribe
Tout savoir sur nos offres

Partagez cette publication

Preprocessor Directives
This example is called lessons.c. Its purpose is to print in the screen one of two lessons. The output will
be either
Programming Principles II
#########
#######
#
#
########
#######
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#########
#######
#
#
########
#######
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#########
#
########
#
########
#######
Or
Computer Architecture
#########
#######
#
########
#######
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#########
#######
#
########
#######
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#########
#
########
########
#######
#
The whole program is listed below.
//-----------------------------------------------------------
//This program is for the EPL132 - Laboratory lesson 3 and
//aims at showing the usage of IFDEF preprocessor statements
//
//Author: Kyriakos Stavrou (TSIK)
//email : tsik@cs.ucy.ac.cy
//-----------------------------------------------------------
//Uncomment this line for EPL132 and comment the next one
#define EPL321
//#define EPL132
int main()
{
#ifdef EPL132
printf(" \n
\n");
printf(" Programming Principles II
\n\n");
printf(" #########
#######
#
#
########
####### \n");
printf(" #
#
#
#
#
#
# \n");
printf(" #
#
#
#
#
#
# \n");
printf(" #
#
#
#
#
#
# \n");
printf(" #########
#######
#
#
########
####### \n");
printf(" #
#
#
#
#
#
\n");
printf(" #
#
#
#
#
#
\n");
printf(" #
#
#
#
#
#
\n");
printf(" #########
#
########
#
########
####### \n");
#endif
#ifdef EPL321
printf(" Computer Architecture
\n\n");
printf(" #########
#######
#
########
#######
# \n");
printf(" #
#
#
#
#
#
# \n");
printf(" #
#
#
#
#
#
# \n");
printf(" #
#
#
#
#
#
# \n");
printf(" #########
#######
#
########
#######
# \n");
printf(" #
#
#
#
#
# \n");
printf(" #
#
#
#
#
# \n");
printf(" #
#
#
#
#
# \n");
printf(" #########
#
########
########
#######
# \n");
#endif
}
In this example you can see two "defines":
#define EPL321
#define EPL132
Case 1
#define EPL321
//#define EPL132
When the program has the line #define EPL321 uncommented and the line #define EPL132 commented,
shaded code will be excluded from the final code to be compiled
//-----------------------------------------------------------
//This program is for the EPL132 - Laboratory lesson 3 and
//aims at showing the usage of IFDEF preprocessor statements
//
//Author: Kyriakos Stavrou (TSIK)
//email : tsik@cs.ucy.ac.cy
//-----------------------------------------------------------
//Uncomment this line for EPL132 and comment the next one
#define EPL321
//#define EPL132
int main()
{
#ifdef EPL132
printf(" \n
\n");
printf(" Programming Principles II
\n\n");
printf(" #########
#######
#
#
########
####### \n");
printf(" #
#
#
#
#
#
# \n");
printf(" #
#
#
#
#
#
# \n");
printf(" #
#
#
#
#
#
# \n");
printf(" #########
#######
#
#
########
####### \n");
printf(" #
#
#
#
#
#
\n");
printf(" #
#
#
#
#
#
\n");
printf(" #
#
#
#
#
#
\n");
printf(" #########
#
########
#
########
####### \n");
#endif
#ifdef EPL321
printf(" Computer Architecture
\n\n");
printf(" #########
#######
#
########
#######
# \n");
printf(" #
#
#
#
#
#
# \n");
printf(" #
#
#
#
#
#
# \n");
printf(" #
#
#
#
#
#
# \n");
printf(" #########
#######
#
########
#######
# \n");
printf(" #
#
#
#
#
# \n");
printf(" #
#
#
#
#
# \n");
printf(" #
#
#
#
#
# \n");
printf(" #########
#
########
########
#######
# \n");
#endif
}
The output of the program will be:
Computer Architecture
#########
#######
#
########
#######
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#########
#######
#
########
#######
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#########
#
########
########
#######
#
Case 2
//#define EPL321
#define EPL132
In the case that the line #define EPL321 comented and the line #define EPL132 uncommented, the code
that will be excluded is the one shown as shaded in the following listing.
//-----------------------------------------------------------
//This program is for the EPL132 - Laboratory lesson 3 and
//aims at showing the usage of IFDEF preprocessor statements
//
//Author: Kyriakos Stavrou (TSIK)
//email : tsik@cs.ucy.ac.cy
//-----------------------------------------------------------
//Uncomment this line for EPL132 and comment the next one
//#define EPL321
#define EPL132
int main()
{
#ifdef EPL132
printf(" \n
\n");
printf(" Programming Principles II
\n\n");
printf(" #########
#######
#
#
########
####### \n");
printf(" #
#
#
#
#
#
# \n");
printf(" #
#
#
#
#
#
# \n");
printf(" #
#
#
#
#
#
# \n");
printf(" #########
#######
#
#
########
####### \n");
printf(" #
#
#
#
#
#
\n");
printf(" #
#
#
#
#
#
\n");
printf(" #
#
#
#
#
#
\n");
printf(" #########
#
########
#
########
####### \n");
#endif
#ifdef EPL321
printf(" Computer Architecture
\n\n");
printf(" #########
#######
#
########
#######
# \n");
printf(" #
#
#
#
#
#
# \n");
printf(" #
#
#
#
#
#
# \n");
printf(" #
#
#
#
#
#
# \n");
printf(" #########
#######
#
########
#######
# \n");
printf(" #
#
#
#
#
# \n");
printf(" #
#
#
#
#
# \n");
printf(" #
#
#
#
#
# \n");
printf(" #########
#
########
########
#######
# \n");
#endif
}
The output of the program will be:
Programming Principles II
#########
#######
#
#
########
#######
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#########
#######
#
#
########
#######
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#########
#
########
#
########
#######
Case 3
//#define EPL132
//#define EPL132
In the case that the line #define EPL321 comented and the line #define EPL132 uncommented, the code
that will be excluded is the one shown as shaded in the following listing.
//-----------------------------------------------------------
//This program is for the EPL132 - Laboratory lesson 3 and
//aims at showing the usage of IFDEF preprocessor statements
//
//Author: Kyriakos Stavrou (TSIK)
//email : tsik@cs.ucy.ac.cy
//-----------------------------------------------------------
//Uncomment this line for EPL132 and comment the next one
//#define EPL321
//#define EPL132
int main()
{
#ifdef EPL132
printf(" \n
\n");
printf(" Programming Principles II
\n\n");
printf(" #########
#######
#
#
########
####### \n");
printf(" #
#
#
#
#
#
# \n");
printf(" #
#
#
#
#
#
# \n");
printf(" #
#
#
#
#
#
# \n");
printf(" #########
#######
#
#
########
####### \n");
printf(" #
#
#
#
#
#
\n");
printf(" #
#
#
#
#
#
\n");
printf(" #
#
#
#
#
#
\n");
printf(" #########
#
########
#
########
####### \n");
#endif
#ifdef EPL321
printf(" Computer Architecture
\n\n");
printf(" #########
#######
#
########
#######
# \n");
printf(" #
#
#
#
#
#
# \n");
printf(" #
#
#
#
#
#
# \n");
printf(" #
#
#
#
#
#
# \n");
printf(" #########
#######
#
########
#######
# \n");
printf(" #
#
#
#
#
# \n");
printf(" #
#
#
#
#
# \n");
printf(" #
#
#
#
#
# \n");
printf(" #########
#
########
########
#######
# \n");
#endif
}
The output of the program will be:
This is because all print commands are excluded from the final code to be compiled.
Case 4
#define EPL321
#define EPL132
In the case that the line #define EPL321 comented and the line #define EPL132 uncommented, the code
that will be excluded is the one shown as shaded in the following listing.
//-----------------------------------------------------------
//This program is for the EPL132 - Laboratory lesson 3 and
//aims at showing the usage of IFDEF preprocessor statements
//
//Author: Kyriakos Stavrou (TSIK)
//email : tsik@cs.ucy.ac.cy
//-----------------------------------------------------------
//Uncomment this line for EPL132 and comment the next one
#define EPL321
#define EPL132
int main()
{
#ifdef EPL132
printf(" \n
\n");
printf(" Programming Principles II
\n\n");
printf(" #########
#######
#
#
########
####### \n");
printf(" #
#
#
#
#
#
# \n");
printf(" #
#
#
#
#
#
# \n");
printf(" #
#
#
#
#
#
# \n");
printf(" #########
#######
#
#
########
####### \n");
printf(" #
#
#
#
#
#
\n");
printf(" #
#
#
#
#
#
\n");
printf(" #
#
#
#
#
#
\n");
printf(" #########
#
########
#
########
####### \n");
#endif
#ifdef EPL321
printf(" Computer Architecture
\n\n");
printf(" #########
#######
#
########
#######
# \n");
printf(" #
#
#
#
#
#
# \n");
printf(" #
#
#
#
#
#
# \n");
printf(" #
#
#
#
#
#
# \n");
printf(" #########
#######
#
########
#######
# \n");
printf(" #
#
#
#
#
# \n");
printf(" #
#
#
#
#
# \n");
printf(" #
#
#
#
#
# \n");
printf(" #########
#
########
########
#######
# \n");
#endif
}
The output of the program will be:
Programming Principles II
#########
#######
#
#
########
#######
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#########
#######
#
#
########
#######
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#########
#
########
#
########
#######
Computer Architecture
#########
#######
#
########
#######
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#########
#######
#
########
#######
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#########
#
########
########
#######
#
Un pour Un
Permettre à tous d'accéder à la lecture
Pour chaque accès à la bibliothèque, YouScribe donne un accès à une personne dans le besoin