Professional Documents
Culture Documents
Chapter6-Preprocessing and IO
Chapter6-Preprocessing and IO
Chapter6-Preprocessing and IO
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:
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
slide 8
Preprocessor
❖ Example for #define
#include <stdio.h>
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
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:
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
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
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];
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
#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>
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>
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
❖ 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