Chapter6-Preprocessing and IO

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

Preprocessing and I/O

Yongseok Son
Department of Computer Science and Engineering
Chung-Ang University
Preprocessing
❖ Preprocessing is a way of making text processing with
your C program before they are actually compiled
▪ Before the actual compilation of every C program it is
passed through a preprocessor
▪ The preprocessor looks through the program trying to find
out specific instructions called preprocessor directives
▪ All preprocessor directives begin with the “#” (hash) symbol
❖ The preprocessor is a part of the compiler which performs
preliminary operations to your code before the compiler
sees it

slide 2
Preprocessor
❖ Compiling and preprocessor directives
Source code
(e.g., my.c)
Preprocessor
Preprocessed
Source code
Compiler
Object code
(my.obj)

Library Linker

Executable code
(my.exe)

slide 3
Preprocessor
❖ Preprocessor directives start with ‘#’ character
▪ Different preprocessor directives perform different tasks
▪ We can categorize the preprocessor directives as follows:
• Inclusion directives (e.g., #include)
• Macro definition directives (e.g., #define)
• Conditional compilation directives (e.g., #if)
• Other directives (predefined macros)

slide 4
Preprocessor
❖ Inclusion directive
▪ This inclusion directive is used to include files into the
current file (#include)
▪ The inclusion directive can be used as follows:

#include <stdio.h> includes stdio header file


includes my.h file from
#include “my.h”
current working directory

▪ Including standard header files


• #include <filename>
▪ User defined header files are written in this way
• #include “filename”

slide 5
Preprocessor
❖ Macro definition directives
▪ In C, the #define directives allows the definition of macros
within your source code
▪ These macro definitions allow constant values to be
declared for use throughout your code
▪ Macro definitions are not variables and cannot be changed
by your program code like variables
▪ You generally use this syntax when creating constants that
represent numbers, string, and expressions

slide 6
Preprocessor
❖ Macro definition directives
▪ Syntax
• The syntax for creating a constant using #define in C
• #define CNAME value
• #define CNAME (expression)
✓ CNAME
▪ The name of the constant
▪ Most C programmers define their constant names in uppercase, but it is not
a requirement of the C language
✓ Value
▪ The value of the constant
✓ Expression
▪ Expression whose value is assigned to the constant

slide 7
Preprocessor
❖ Macro definition directives
▪ Example of #define
• Number
✓ The following is an example of how you use the #define
directive to define a numeric constant

#define AGE 20

✓ In this example, the constant named AGE contains the


value of 20
• String
✓ You can use the #define directive to define a string constant

#define NAME “Computer Programming”

✓ In this example, the constant called NAME contains the


value of “Computer Programming”

slide 8
Preprocessor
❖ Example for #define
#include <stdio.h>

#define NAME “Computer Programming”


#define AGE 20

int main()
{
printf(‘%s is over %d years old.\n”, NAME, AGE);
return 0;
}

slide 9
Preprocessor
❖ Example of #define
▪ Expression
• You can use the #define directive to define a constant using
an expression

#define AGE (20 / 2)

• In this example, the constant named AGE contain the value


of 10

slide 10
Preprocessor
❖ Parameterized Macros
▪ One of the powerful functions is the ability to simulate
functions using parameterized macros
▪ For example, we might have some code to square a
number as follows:

int square(int x){ #include <stdio.h>


return x * x;
#define MAX(x,y) ((x) > (y) ? (x) : (y))
}
int main(void) {
printf("Max between 20 and 10 is %d\n", MAX(10, 20));
return 0;
}

#define square(x) ((x) * (x))

slide 11
Preprocessor
❖ Conditional compilation directives
▪ Conditional inclusions (#ifdef, #ifndef, #if, #endif, #else, #elif)
• #ifdef directive
#ifdef MACRO
conditional codes
#endif

• #if directive

#if expression
conditional codes
#endif

slide 12
Preprocessor
❖ Conditional compilation directives
▪ Conditional inclusions (#ifdef, #ifndef, #if, #endif, #else, #elif)
• #if, #else, #endif directives
#if expression
conditional codes if expression is non-zero
#else
conditional codes if expression is 0
#endif

• #if, #elif, #else, #endif directives


#if expression
conditional codes if expression is non-zero
#elif expression1
conditional codes else if expression is non-zero

#else
conditional codes if all expressions are 0
#endif

slide 13
Preprocessor
❖ Conditional compilation directives
▪ Conditional inclusions (#ifdef, #ifndef, #if, #endif, #else,
#elif)
▪ These directives allow to include or discard part of the
code of a program if a certain condition is met
• #ifdef allows a section of a program to be compiled only if
the macro that is specified as the parameter has been
defined

#ifdef TABLE_SIZE
int table[TABLE_SIZE];
#endif

• In this case, the line of code “int table[TABLE_SIZE];” is only


compiled if TABLE_SIZE was previously defined with #define
✓ If it was not defined, that line will not be included in the program
compilation

slide 14
Preprocessor
❖ Conditional compilation directives
▪ #ifndef serves for the exact opposite: the code between
#ifndef and #endif directives is only compiled if the
specified identifier has not been previously defined

#ifndef TABLE_SIZE
#define TABLE_SIZE 100
#endif
int table[TABLE_SIZE];

▪ In this case, when arriving at this piece of code, the


TABLE_SIZE macro has not been defined yet, it would be
defined to a value of 100
• If it alreay existed it would keep its previous value since the
#define directive would not be executed

slide 15
Preprocessor
❖ Conditional compilation directives
▪ The #if, #else, and #elif (i.e., “else if”) directives serve to
specify some condition to be met in order for the portion of
code to be compiled
▪ Example
#if TABLE_SIZE > 200
#undef TABLE_SIZE
#define TABLE_SIZE 200

#elif TABLE_SIZE < 50


#undef TABLE_SIZE
#define TABLE_SIZE 50

#else
#undef TABLE_SIZE
#define TABLE_SIZE 100
#endif

int table[TABLE_SIZE];

slide 16
Preprocessor
❖ #undef
▪ In C, the #undef directive tells the preprocessor to remove
all definitions for the specified macro
▪ A macro can be redefined after it has been removed by the
#undef directive
▪ Syntax
• The syntax for the #undef directive in C

#undef macro_definition

slide 17
Preprocessor
❖ Example of #undef
▪ In this example, the YEARS_OLD macro is first defined with a
value of 20 and then undefined using the #undef directive
▪ Since the macro no longer exists, the statement #ifdef
YEARS_OLD evaluates to false
• This causes the subsequent printf function to be skipped

#include <stdio.h>
#define YEARS_OLD 20
#undef YEARS_OLD

int main()
{
#ifdef YEARS_OLD
printf(“Computer Programming is over %d years old.\n", YEARS_OLD);
#endif

return 0;
}

slide 18
Preprocessor
❖ The Defined() Operator
▪ The defined operator is used in constant expressions to
determine if an identifier is defined using #define
• If the specified identifier is defined, the value is true.
• If it is not defined, the value is false

#include <stdio.h>

#if !defined (MESSAGE)


#define MESSAGE "You wish!"
#endif

int main(void) {
printf("Here is the message: %s\n", MESSAGE);
return 0;
}

slide 19
Preprocessor
❖ Predefined Macro
▪ __DATE__
• The current date as a character literal in “MMM DD YYYY” format
▪ __TIME__
• The current time as a character literal in “HH:MM:SS” format
▪ __FILE__
• This contains the current filename as a string literal
▪ __LINE__
• This contains the current line number as a decimal constant

#include <stdio.h>

int main() { File: test.c


printf("File: %s\n", __FILE__ ); Date: Jun 2 2018
printf("Date: %s\n", __DATE__ ); Time: 03:36:24
printf("Time: %s\n", __TIME__ ); Line: 8
printf("Line: %d\n", __LINE__ );
}

slide 20
Standard Input/Output (I/O)
❖ Pre-connected input and output channels between a
computer program and its environment (typically a text
terminal)
▪ Standard input
• Text input from keyboard
▪ Standard output
• Text output written to display
▪ Standard error
• Another text output written to display for error message

slide 21
Standard I/O Library
❖ Library
▪ A collection of subroutines (functions) used to develop
software
❖ Standard library
▪ Library that is made available in every implementation of a
programming language
▪ Same interface (parameter type), same functionality in
different systems
❖ Standard I/O library
▪ Standard library for processing I/O

slide 22
Printf Function
❖ Printf function

printf(control string, argument list);

❖ Control string contains


▪ Literal text to be displayed
▪ Format specifiers
▪ Special characters

❖ Arguments can be
▪ Variable, function, expression, constant
▪ The number of argument list must match the number of
format specifiers

slide 23
Scanf Function
❖ The scanf function allow you to accept input

scanf(“%d”, &b);

❖ The program will read in an integer value that the user enters on the
keyboard (%d is for integers) so “b” must be declared as an “int”
❖ The scanf function uses the same format as printf:
▪ int uses %d
▪ float uses %f
▪ char uses %c
▪ character strings use %s

slide 24
Scanf Function
❖ The scanf function internally accepts an integer from the
user and then accesses and assigns the variable
▪ In order to access local variables declared in main function
in scanf function, we need to know the address of the
variable
▪ So we are calling the scanf function and passing the
address value of the local variable val to be filled in as an
argument

#include <stdio.h>
int main(void){
int val;
scanf(“%d”, &val);
return 0;
}

slide 25
gets(), puts() functions
❖ String I/O functions
❖ Prototype
▪ char* gets(char *buf);
• Read characters from standard input until a newline is found
▪ int puts(const char *s);
• Write a string s to the standard output
#include <stdio.h>
#define MAX_LINES 256

int main()
{
char line[MAX_LINES];
printf(“string input :”);
gets(line);
printf(“the input string is : ”);
puts(line);

return 0;
}

slide 26

You might also like