3 1. preprocessor, math, stdlib

17
Preprocessor Preprocessor, math.h stdlib.h

Upload: -

Post on 25-May-2015

969 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 3 1. preprocessor, math, stdlib

Preprocessor

Preprocessor, math.hstdlib.h

Page 2: 3 1. preprocessor, math, stdlib

2

Preprocessor

Preprocessor– Permits to define simple macros that are evaluated and

expanded prior to compilation.– Commands begin with a ‘#’.

– #define : defines a macro– #undef : removes a macro definition– #include : insert text from file– #if : conditional based on value of expression– #ifdef : conditional based on whether macro defined– #ifndef : conditional based on whether macro is not defined– #else : alternative– #elif : conditional alternative– defined() : preprocessor function: 1 if name defined, else 0

#if defined(__NetBSD__)

[Ex] #include <stdio.h>#define PI 3.14159

Page 3: 3 1. preprocessor, math, stdlib

3

Preprocessor

[Ex]#include <stdio.h>#include “./test/file.h”

Include file.h in test directory.

Include stdio.h which is in the default directory

#include <filename> or #include “filename”– Include contents of filename in this position (not modify your

source file, but make a new temporary file just before compile)– <> is used if filename is in the system default directory

(Most of the standard header files are in the default directory)– “” is used if filename is not in the default directory

(Write the path to filename if it is not in the current directory)

Page 4: 3 1. preprocessor, math, stdlib

Preprocessor

Header file– Header files with the extension “.h” are included by #include

Contents of Header file– Prototype of Function– ‘extern’ global variables– type definition, etc.

Typical header files– stdio.h, stdlib.h, math.h, etc.

4

Refer to Modulization

Page 5: 3 1. preprocessor, math, stdlib

5

Preprocessor

[Ex]

#define LIMIT 100#define PI 3.14159

#define [identifier] [replacement]– replaces any occurrence of identifier in the rest of the code by replacement.

– replacement can be an expression, a statement, a block or simply anything.

Preprocessor regards LIMIT as 100,PI as 3.141592.

Page 6: 3 1. preprocessor, math, stdlib

6

Preprocessor

#include <stdio.h>#define LIMIT 100#define PI 3.14159void main(void){

printf( “%d, %f\n”, LIMIT, PI ) ;}

#define

…void main(void){

printf( “%d, %f\n”, 100, 3.14159 ) ;}

Preprocessor makes a temporary file.

After that, compile the temporary file.

Page 7: 3 1. preprocessor, math, stdlib

7

Preprocessor

Example

#include <stdio.h>

#define YELLOW 0#define RED 1#define BLUE 2

void main(void){

int color ;

for( color = YELLOW ; color <= BLUE ; color++ ) {switch( color ) {case YELLOW : printf( “Yellow\n” ) ; break ;case RED : printf( “Red\n” ) ; break ;case BLUE : printf( “Blue\n” ) ; break ;}

}}

Page 8: 3 1. preprocessor, math, stdlib

8

Preprocessor Macro function: Declare a function by using #define

– Compiled after the code modified by preprocessor as follows.

#define multiply(a,b) ((a)*(b))

void main() {int c = multiply(3,2) ;

}

void main() {int c = ((3)*(2)) ;

}

Page 9: 3 1. preprocessor, math, stdlib

9

Preprocessor Macro function: Be careful! Simple replacement!!

– what will happen?

– Compiled after the code modified by preprocessor as follows.

– Use ‘()’ for safety.

#define multiply(a,b) a*b

void main() {int c = multiply(3+1,2+2) ;

}

void main() {int c = 3+1*2+2 ;

}

Page 10: 3 1. preprocessor, math, stdlib

Math functions

(math.h)

Page 11: 3 1. preprocessor, math, stdlib

11

Mathematical Functions

Mathematical Functions

– sqrt(), pow(), exp(), log(), sin(), cos(), tan(), etc.

– Include the Header file <math.h>

– All the math functions have argument with double type andreturn value with double type.

– Give “-lm” flag when you compile this with Unix(gcc).

[Ex] gcc filename.c -lm

[Ex] double sin(double); /* argument is the angle in radians */double pow(double, double);

Page 12: 3 1. preprocessor, math, stdlib

12

Mathematical Functions

[Ex] #include <stdio.h>#include <math.h>

#define PI 3.1415926

int main() {double r = PI / (2*90); /* 180 radian = 1 degree */int i;

for(i=0; i<=90; i+=5) {printf("cos(%d) = %f\t", i, cos(r*i));printf("sin(%d) = %f\t", i, sin(r*i));printf("tan(%d) = %f\n", i, tan(r*i));

}}

cos(0) = 1.000000 sin(0) = 0.000000 tan(0) = 0.000000…cos(45) = 0.707107 sin(45) = 0.707107 tan(45) = 1.000000…cos(90) = 0.000000 sin(90) = 1.000000 tan(90) = 37320539.634355

Page 13: 3 1. preprocessor, math, stdlib

Misc. functions

(stdlib.h)

Page 14: 3 1. preprocessor, math, stdlib

Miscellaneous functions

Standard Headers you should know about: – stdio.h: file and console (also a file) IO

• perror, printf, open, close, read, write, scanf, etc.

– stdlib.h: common utility functions• malloc, calloc, strtol, atoi, etc

– string.h: string and byte manipulation• strlen, strcpy, strcat, memcpy, memset, etc.

– math.h: math functions• ceil, exp, floor, sqrt, etc.

14

Page 15: 3 1. preprocessor, math, stdlib

Miscellaneous functions

Standard Headers you should know about: – ctype.h: character types

• isalnum, isprint, isupport, tolower, etc.

– errno.h: defines errno used for reporting system errors– signal.h: signal handling facility

• raise, signal, etc

– stdint.h: standard integer• intN_t, uintN_t, etc

– time.h: time related facility• asctime, clock, time_t, etc.

15

Page 16: 3 1. preprocessor, math, stdlib

16

Miscellaneous functions

Miscellaneous Functions

– atoi(), rand(), srand()

– <stdlib.h>: Common Utility Functions

– atoi(“String”): Convert string to integer

– rand() : Generate random number

– srand( Initial Value ) : Initialize random number generator

int a = atoi( “1234” ) ;

int a = rand() ;

srand( 3 ) ;

Page 17: 3 1. preprocessor, math, stdlib

Ex.: Print 10 random numbers

Print different random number in every execution

17

Miscellaneous functions

#include <stdio.h>#include <stdlib.h>

int main() {for(i=0; i<10; i++)

printf(“%d\n”, rand() ) ;}

Run this Program twice

#include <stdio.h>#include <stdlib.h>#include <time.h>

int main() {srand( (int)time(NULL) ) ;for(i=0; i<10; i++)

printf(“%d\n”, rand() ) ;}