Preprocessor directives tutorial

icon

5

pages

icon

English

icon

Documents

Écrit par

Publié par

Le téléchargement nécessite un accès à la bibliothèque YouScribe Tout savoir sur nos offres

icon

5

pages

icon

English

icon

Ebook

Le téléchargement nécessite un accès à la bibliothèque YouScribe Tout savoir sur nos offres

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(" ######### ####### # # ...
Voir Alternate Text

Publié par

Nombre de lectures

89

Langue

English

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
#########
#######
#
########
#######
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#########
#######
#
########
#######
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#########
#
########
########
#######
#
Voir Alternate Text
  • Univers Univers
  • Ebooks Ebooks
  • Livres audio Livres audio
  • Presse Presse
  • Podcasts Podcasts
  • BD BD
  • Documents Documents
Alternate Text