Download presentation
Presentation is loading. Please wait.
Published byGabriella Maxwell Modified over 9 years ago
1
CGS 3460 Preprocessor n All preprocessor directives or commands begin with a #. lE.g. #include C program → Modified C program → Object Code n Can appear anywhere in the program n No “;” in the end preprocessor compiler
2
CGS 3460 Preprocessor Directives n Macro definition l#define, #undef n File inclusion l#include n Conditional Compilation l#if, #ifdef, #ifndef, #elseif, #else
3
CGS 3460 Advantages n Easy to lDevelop program lRead programs lModify programs n C code more transportable between different machine architectures
4
CGS 3460 #define n To define constants or any macro substitution. #define E.g. #define FALSE 0 #define TRUE !FALSE n To undefined a macro. E.g. #undef FALSE lA macro must be undefined before being redefined to a different value.
5
CGS 3460 Define Functions n E.g. To get the maximum of two variables: #define max(A,B) ( (A) > (B) ? (A):(B)) n If in the C code: x = max(q+r,s+t); n After preprocessing: x = ( (q+r) > (s+t) ? (q+r) : (s+t));
6
CGS 3460 File inclusion n #include directive lInclude the contents of another file at the point where the directive appears. n Why need file inclusion lUse built-in functions E.g., printf(), rand(); lReuse 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
7
CGS 3460 File inclusion formats n #include lUsed for system header files lFile contain function prototypes for library functions,, etc n #include "file" lUsed for header files of your own program looks for a file in the current directory first then system directories if not found
8
CGS 3460 Standard C libraries n Built-in with your compiler n Detailed information lhttp://www.utas.edu.au/infosys/info/documentation/C/CStdLib.htmlhttp://www.utas.edu.au/infosys/info/documentation/C/CStdLib.html n stdio.h lcore input and output capabilities of C printf functionprintf scanf function
9
CGS 3460 Math Library Functions n Math library functions lperform common mathematical calculations E.g. exp(), pow(), sqrt(), fabs(), sin(), cos(). l#include n Format for calling functions lFunctionName( argument, …, argument ); All math functions return data type double lE.g.: printf( "%.2f", sqrt( 900.0 ) ); lArguments may be constants, variables, or expressions n Compile lgcc yourfilename.c –lm –o yourfilename.exe
10
CGS 3460 stdlib.h n A variety of utility functions lrand function A function to generate a pseudo-random integer number Return value is in the range 0 to RAND_MAX Example: #include int i = rand(); lmemory allocation lprocess control
11
CGS 3460 string.h n All the string handling functions lstrcpy lstrcat lstrcmp
12
CGS 3460 Custom header files n Steps for creating custom header files lCreate a file with function prototypes Save as a.h file. E.g.: filename.h lLoad in other files with #include "filename.h" n Advantage lReuse functions and data structure declaration
13
CGS 3460 Conditional compilation n Useful when lmachine-dependencies lDebugging lsetting certain options at compile-time. n Expressions l#if expression l#ifdef expression (same as #if defined ) l#ifudef l#elif and #else l#endif
14
CGS 3460 Examples n Example: write programs that are portable to several machines or operating systems l#if defined(WINDOWS) l#elif defined(LINUX) l#elif defined(SOLARIS) l#endif n Example: Providing a default definition for a macro l#ifndef BUFFER_SIZE l#define BUFFER_SIZE 256 l#endif
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.