Preprocessor directives tutorial
5 Pages
English

Preprocessor directives tutorial

-

Downloading requires you to have access to the YouScribe library
Learn all about the services we offer

Description

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

Subjects

Informations

Published by
Reads 89
Language 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
#########
#######
#
########
#######
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#########
#######
#
########
#######
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#########
#
########
########
#######
#