Preprocessor All preprocessor directives or commands begin with a #. –E.g. #include C program →...
-
Upload
alberta-pitts -
Category
Documents
-
view
216 -
download
2
Transcript of Preprocessor All preprocessor directives or commands begin with a #. –E.g. #include C program →...
![Page 1: Preprocessor All preprocessor directives or commands begin with a #. –E.g. #include C program → Modified C program → Object Code Can appear anywhere in.](https://reader035.fdocuments.in/reader035/viewer/2022081002/56649dd25503460f94ac93c0/html5/thumbnails/1.jpg)
Preprocessor
• All preprocessor directives or commands begin with a #.– E.g. #include <stdio.h>
C program → Modified C program → Object Code
• Can appear anywhere in the program• No “;” in the end
preprocessor compiler
![Page 2: Preprocessor All preprocessor directives or commands begin with a #. –E.g. #include C program → Modified C program → Object Code Can appear anywhere in.](https://reader035.fdocuments.in/reader035/viewer/2022081002/56649dd25503460f94ac93c0/html5/thumbnails/2.jpg)
Preprocessor Directives
• Macro definition– #define, #undef
• File inclusion– #include
• Conditional Compilation– #if, #ifdef, #ifndef, #elseif, #else
• Others
![Page 3: Preprocessor All preprocessor directives or commands begin with a #. –E.g. #include C program → Modified C program → Object Code Can appear anywhere in.](https://reader035.fdocuments.in/reader035/viewer/2022081002/56649dd25503460f94ac93c0/html5/thumbnails/3.jpg)
Advantages
• Easy to– Develop program – Read programs – Modify programs
• C code more transportable between different machine architectures
![Page 4: Preprocessor All preprocessor directives or commands begin with a #. –E.g. #include C program → Modified C program → Object Code Can appear anywhere in.](https://reader035.fdocuments.in/reader035/viewer/2022081002/56649dd25503460f94ac93c0/html5/thumbnails/4.jpg)
#define
• To define constants or any macro substitution.
#define <macro> <replacement name>
E.g.
#define FALSE 0
#define TRUE !FALSE
• To undefined a macro.
E.g. #undef FALSE
– A macro must be undefined before being redefined to a different value.
![Page 5: Preprocessor All preprocessor directives or commands begin with a #. –E.g. #include C program → Modified C program → Object Code Can appear anywhere in.](https://reader035.fdocuments.in/reader035/viewer/2022081002/56649dd25503460f94ac93c0/html5/thumbnails/5.jpg)
Define Functions
• E.g. To get the maximum of two variables:
#define max(A,B) ( (A) > (B) ? (A):(B))
• If in the C code: x = max(q+r,s+t);
• After preprocessing:
x = ( (q+r) > (s+t) ? (q+r) : (s+t));
![Page 6: Preprocessor All preprocessor directives or commands begin with a #. –E.g. #include C program → Modified C program → Object Code Can appear anywhere in.](https://reader035.fdocuments.in/reader035/viewer/2022081002/56649dd25503460f94ac93c0/html5/thumbnails/6.jpg)
File inclusion
• #include directive– Include the contents of another file at the point where the
directive appears.
• Why need file inclusion– Use built-in functions
• E.g., printf(), rand();
– Reuse code
defTime.h
struct date{int day;char month[10];int year;
};
struct date{int day;char month[10];int year;
};
struct date today;
#include “defTime.h”→
struct date today;
Your code
![Page 7: Preprocessor All preprocessor directives or commands begin with a #. –E.g. #include C program → Modified C program → Object Code Can appear anywhere in.](https://reader035.fdocuments.in/reader035/viewer/2022081002/56649dd25503460f94ac93c0/html5/thumbnails/7.jpg)
File inclusion formats
• #include <file> – Used for system header files– File contain function prototypes for library
functions•<stdlib.h> , <math.h> , etc
• #include "file" – Used for header files of your own program
• looks for a file in the current directory first• then system directories if not found
![Page 8: Preprocessor All preprocessor directives or commands begin with a #. –E.g. #include C program → Modified C program → Object Code Can appear anywhere in.](https://reader035.fdocuments.in/reader035/viewer/2022081002/56649dd25503460f94ac93c0/html5/thumbnails/8.jpg)
Standard C libraries
• Build-in with your compiler
• Detailed information– http://www.utas.edu.au/infosys/info/document
ation/C/CStdLib.html
• stdio.h– core input and output capabilities of C
• printf function• scanf function
![Page 9: Preprocessor All preprocessor directives or commands begin with a #. –E.g. #include C program → Modified C program → Object Code Can appear anywhere in.](https://reader035.fdocuments.in/reader035/viewer/2022081002/56649dd25503460f94ac93c0/html5/thumbnails/9.jpg)
Math Library Functions
• Math library functions – perform common mathematical calculations
• E.g. exp(), pow(), sqrt(), fabs(), sin(), cos().– #include <math.h>
• Format for calling functions– FunctionName( argument, …, argument );
• All math functions return data type double– E.g.: printf( "%.2f", sqrt( 900.0 ) ); – Arguments may be constants, variables, or
expressions• Compile
– gcc yourfilename.c –lm –o yourfilename.exe
![Page 10: Preprocessor All preprocessor directives or commands begin with a #. –E.g. #include C program → Modified C program → Object Code Can appear anywhere in.](https://reader035.fdocuments.in/reader035/viewer/2022081002/56649dd25503460f94ac93c0/html5/thumbnails/10.jpg)
stdlib.h
• A variety of utility functions– rand function
• A function to generate a pseudo-random integer number
• Return value is in the range 0 to RAND_MAX • Example:
#include <stdlib.h> int i = rand();
– memory allocation– process control
![Page 11: Preprocessor All preprocessor directives or commands begin with a #. –E.g. #include C program → Modified C program → Object Code Can appear anywhere in.](https://reader035.fdocuments.in/reader035/viewer/2022081002/56649dd25503460f94ac93c0/html5/thumbnails/11.jpg)
string.h
• All the string handling functions – strcpy – strcat – strcmp
![Page 12: Preprocessor All preprocessor directives or commands begin with a #. –E.g. #include C program → Modified C program → Object Code Can appear anywhere in.](https://reader035.fdocuments.in/reader035/viewer/2022081002/56649dd25503460f94ac93c0/html5/thumbnails/12.jpg)
Custom header files
• Steps for creating custom header files– Create a file with function prototypes – Save as a .h file. E.g.: filename.h– Load in other files with
•#include "filename.h"
• Advantage– Reuse functions and data structure
declaration
![Page 13: Preprocessor All preprocessor directives or commands begin with a #. –E.g. #include C program → Modified C program → Object Code Can appear anywhere in.](https://reader035.fdocuments.in/reader035/viewer/2022081002/56649dd25503460f94ac93c0/html5/thumbnails/13.jpg)
Conditional compilation
• Useful when– machine-dependencies– Debugging– setting certain options at compile-time.
• Expressions– #if expression– #ifdef expression (same as #if defined )– #ifudef– #elif and #else– #endif
![Page 14: Preprocessor All preprocessor directives or commands begin with a #. –E.g. #include C program → Modified C program → Object Code Can appear anywhere in.](https://reader035.fdocuments.in/reader035/viewer/2022081002/56649dd25503460f94ac93c0/html5/thumbnails/14.jpg)
Examples
• Example: write programs that are portable to several machines or operating systems– #if defined(WINDOWS)– #elif defined(LINUX)– #elif defined(SOLARIS)– #endif
• Example: Providing a default definition for a macro– #ifndef BUFFER_SIZE– #define BUFFER_SIZE 256– #endif