outline
play

Outline Midterm review More c CS3157: Advanced Preprocessor - PDF document

Outline Midterm review More c CS3157: Advanced Preprocessor Programming Bitwise operations Character handling Lecture #8 Math/random Oct 24 Reading: Shlomo Hershkop k&r ch chapter 4


  1. Outline • Midterm review • More c CS3157: Advanced – Preprocessor Programming – Bitwise operations – Character handling Lecture #8 – Math/random Oct 24 • Reading: Shlomo Hershkop – k&r ch chapter 4 shlomo@cs.columbia.edu – Next class chapter 6. 1 2 Announcements Pre-processor • the C pre-processor (cpp) is a macro-processor which • Midterms graded, will be handed back – manages a collection of macro definitions today. – reads a C program and transforms it • Excellent work on the most part. • pre-processor directives start with # at beginning of line used to: – $1 patterns – include files with C code (typically, “header” files containing – Pass by reference in perl definitions; file names end with .h) – define new macros (later – not today) – Excellent suggestions – conditionally compile parts of file (later – not today) • No lab on Wednesday (10/26). Do reading • gcc -E shows output of pre-processor • can be used independently of compiler 3 4 1

  2. Pre-processor cont. Example #define MAXVALUE 100 #define name const-expression #define check(x) ((x) < MAXVALUE) #define name (param1,param2,...) expression if (check(i)) { ...} #undef symbol • becomes if ((i) < 100) {...} • replaces name with constant or expression • textual substitution • Caution: don’t treat macros like function calls • symbolic names for global constants #define valid(x) ((x) > 0 && (x) < 20) • is called like: • in-line functions (avoid function call overhead) if (valid(x++)) {...} • type-independent code • and will become: valid(x++) -> ((x++) > 0 && (x++) < 20) • and may not do what you intended... #define MAXLEN 255 5 6 • conditional compilation • ifdef • for boolean flags, easier: • pre-processor checks value of expression • if true, outputs code segment 1, otherwise code segment 2 #ifdef name • machine or OS-dependent code code segment 1 • can be used to comment out chunks of code— bad! • (but can be helpful for quick and dirty debugging :-) #else code segment 2 • example: #define OS linux #endif ... #if OS == linux • pre-processor checks if name has been puts( “Wow you are running Linux!" ); defined, e.g.: #else puts( "why are you running something else???" ); #define USEDB #endif • if so, use code segment 1, otherwise 2 7 8 2

  3. Function Command Line Args int main( int argc, char *argv[] ) • Declaration: – Return-type function-name (parameters if any); • Definition: • argc is the argument count – Return-type function-name (parameters if any){ • argv is the argument vector – array of strings with command-line arguments declarations • the int value is the return value statements – convention: return value of 0 means success, } – > 0 means there was some kind of error – can also declare as void (no return value) 9 10 • Name of executable followed by space- • If no arguments, simplify: separated arguments int main() { $ a.out 1 23 "third arg" printf( "hello world" ); • this is stored like this: exit( 0 ); 1. a.out } 2. 1 3. 23 4. “third arg” • Uses exit() instead of return() — almost the same thing. • argc = 4 11 12 3

  4. booleans Booleans II • C doesn’t have booleans • This works in general, but beware: • emulate as int or char, with values 0 (false) and 1 or non- if ( n == TRUE ) { zero (true) printf( "everything is a-okay" ); • allowed by flow control statements: } if ( n == 0 ) { printf( "something wrong" ); • if n is greater than zero, it will be non-zero, but } may not be 1; so the above is NOT the � • assignment returns zero - > false • same as: • you can define your own boolean: if ( n ) { #define FALSE 0 printf( "something is rotten in the state of denmark" ); #define TRUE 1 } 13 14 Logical operators Bitwise operators • in C logical operators are the same as in Java • there are also bitwise operators in C, in which • meaning C operator each bit is an operand: • AND && • Meaning c operator • OR || • bitwise AND & • NOT ! • bitwise or | • since there are no boolean types in C, these are mainly • Example: used to connect clauses in if and while statements int a = 8; /* this is 1000 in base 2 */ • remember that int b = 15; /* this is 1111 in base 2 */ – non-zero == true 1000 ( 8 ) • a & b = a | b= 1000 ( 8 ) | – zero == false & 1111 ( 15 ) 1111 ( 15 ) = = 1000 ( 8 ) 1111 ( 15 ) 15 16 4

  5. Question Implicit convertions • implicit: • what is the output of the following code int a = 1; fragment? char b = 97; // converts int to char int s = a + b; // adds int and char, converts to int • int a = 12, b = 7; • promotion: char -> short -> int -> float -> double • if one operand is double, the other is made double • printf( "a && b = %d\n", a && b ); • else if either is float, the other is made float • printf( "a || b = %d\n", a || b ); int a = 3; float x = 97.6; • printf( "a & b = %d\n", a & b ); double y = 145.987; y = x * y; // x becomes double; result is double • printf( "a | b = %d\n", a | b ); x = x + a; // a becomes float; result is float • real (float or double) to int truncates 17 18 explicit Example • explicit: #include <stdio.h> • type casting #include <math.h> int a = 3; int main() { float x = 97.6; int j, i, x; double y = 145.987; double f = 12.00; y = (double)x * y; for ( j=0; j<10; j++ ) { x = x + (float)a; i = f; • – using functions (in math library...) x = (int)f; printf( "f=%.2f i=%d x=%d 1. floor() – rounds to largest integer not greater than x floor(f)=%.2f ceil(f)=%.2f round(f)=%.2f\n", f,i,x,floor(f),ceil(f),round(f) ); 2. ceil() - round to smallest integer not smaller than x f += 0.10; } // end for j 3. round() – rounds up from halfway integer values } // end main() 19 20 5

  6. Output Be aware • f=12.00 i=12 x=12 floor(f)=12.00 ceil(f)=12.00 round(f)=12.00 • almost any conversion does something— but not • f=12.10 i=12 x=12 floor(f)=12.00 ceil(f)=13.00 round(f)=12.00 necessarily what you intended!! • f=12.20 i=12 x=12 floor(f)=12.00 ceil(f)=13.00 round(f)=12.00 • – example: • f=12.30 i=12 x=12 floor(f)=12.00 ceil(f)=13.00 round(f)=12.00 • f=12.40 i=12 x=12 floor(f)=12.00 ceil(f)=13.00 round(f)=12.00 int x = 100000; • f=12.50 i=12 x=12 floor(f)=12.00 ceil(f)=13.00 round(f)=12.00 • f=12.60 i=12 x=12 floor(f)=12.00 ceil(f)=13.00 round(f)=13.00 short s = x; • f=12.70 i=12 x=12 floor(f)=12.00 ceil(f)=13.00 round(f)=13.00 printf("%d %d\n", x, s); • f=12.80 i=12 x=12 floor(f)=12.00 ceil(f)=13.00 round(f)=13.00 • f=12.90 i=12 x=12 floor(f)=12.00 ceil(f)=13.00 round(f)=13.00 • – output is: 100000 -31072 • WHY? 21 22 math library math • Functions ceil() and floor() come from the math library • some other functions from the math library (these are function prototypes): • definitions: – double sqrt( double x ); – ceil( x ): returns the smallest integer not less than x, as a double – double pow( double x, double y ); – floor( x ): returns the largest integer not greater than x, as a – double exp( double x ); double – double log( double x ); • in order to use these functions, you need to do two – double sin( double x ); things: – double cos( double x ); 1. include the prototypes (i.e., function definitions) in the • exercise: write a program that calls each of these functions source code: #include <math.h> • questions: 2. include the library (i.e., functions’ object code) at link – can you make sense of /usr/include/math.h? time: – where are the definitions of the above functions? unix$ gcc abcd.c -lm – what are other math library functions? • exercise: can you write a program that rounds a floating point? 23 24 6

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend