210 likes | 326 Views
Week 2-3. Control flow (review) Conditional statements If, else, else if, switch-case, break Loop constructs for, while, do-while, break, continue, label--go; Function definition Return type, parameter definition, parameter passing Function calls and standard library Program Structures
E N D
Week 2-3 • Control flow (review) • Conditional statements • If, else, else if, switch-case, break • Loop constructs • for, while, do-while, break, continue, label--go; • Function definition • Return type, parameter definition, parameter passing • Function calls and standard library • Program Structures • Preprocessing • Header files, Application Programming Interfaces (API) • Scope rules • C library
Conditional statements • If, else, else if, • http://www.cse.ohio-state.edu/~caod/cse45921/coding/ifelse_binarysearch.c • switch-case, break • http://www.cse.ohio-state.edu/~caod/cse45921/coding/switch.c
Control flow • Loop constructs • for • while • do-while • http://www.cse.ohio-state.edu/~caod/cse45921/coding/for.c • Other flow control methods • break • continue • label—go • http://www.cse.ohio-state.edu/~caod/cse45921/coding/switch.c • Sample programs • calculate the sum of 1-10 using different loops • http://www.cse.ohio-state.edu/~caod/cse45921/coding/Sample_sum.c
Function • Function prototype • return type • argument definition • Function calls • basic syntax • parameter passing • Example: find the next prime number
Function Organization • A set of functions • Defined in the same files as main() • Add two more interfaces: • my_isuppercase() and my_islowercase() • Sample program • Use separated file • Header files: User-defined: “…”, System: <...> • How to modify the above sample • multi_check.h • check.c • multi.c • main.c • Separated compilation
Recursion • A function can call itself • Directly • Indirectly • examples: • Calculate the power of x to y • Find a matching number in an increasing array of integers int Power(int x, int y) { int pow=1; int i=0; for (i=0;i<y;y++) { pow=pow*x; return pow; } } int Power(int x, int y) { if(y==0) return 1; else return x*Power(x,y-1); }
Scope Rules • Scope: • Where the variable or function definitions can be accessed • Local and global variables • Local: a variable defined inside a function • Global: a variable defined outside of functions • external variable • Note: global variable and external variables • Typically used when a global variable is used in another file
Scope Rules -- continued • static variable • Value are retained on exit from function • Limited scope • Only accessible within the same file • Or other source files that ‘include’s this file • Automatic variables: • Typically local variables • variables that are automatically created/destroyed • Register variable: • variables are saved in register for fast access
Scope Rules -- Example //multi.c #include “multi_check.h” extern int z; int multiplication(int x, int y) { static int cnt=0; cnt=cnt+1; printf(“%dth invocation”, cnt); z=x*y; return z; } //check.c extern int z; #include “multi_check.h” int check(int x) { if(x>0) return 1; else return 0; } //main.c int z=0; #include <stdio.h> #include “multi_check.h” int main() { int x, y; printf(“please input two postive integers); scanf(“%d,%d”,&x,&y); if(check(x)&&check(y)){ z=multiplication(x,y); // 1th invocation z=multiplication(x,y); //2th invocation printf(“%d”,z); } else{ printf(“negative inputs!”); } return 0; }
Function scope • All functions are defined externally • Static function, limited scope • Inline functions • The complier will perform inline expansion: inserting the body of function every place it is invoked rather than generating code to call the function in the one place it is defined. • Eliminate the time overhead when a function is called. Typically used for functions that execute frequently.
Preprocessing • Preprocessing directives • file inclusion • Allow a program to use certain interfaces • #include • code replacement • #define • Conditional compilation • One source for many platforms • #ifdef -- #endif; #if expr -- #endif • Conditional inclusion • #ifdef #include #endif #if SYSTEM == SYSV #define HDR "sysv.h" #elif SYSTEM == BSD #define HDR "bsd.h" #elif SYSTEM == MSDOS #define HDR "msdos.h" #else #define HDR "default.h" #endif #include HDR gcc –DSYSV test.c
Types of lines in Makefiles • Dependency or rules lines • Commands • Macro assignments • Comments
Dependency/rules lines • Specify a target and a list of prerequisites (optional) for that target target : prereq1 prereq2 prereq3 …
Command lines • Follow dependency lines • MUST start with a TAB! • Any command that can be run in the shell can be placed here target : prereq1 prereq2 command1 command2 • Special variables in commands: • $@ represents the target • $? represents prereqs that are newer than target
Simplified Makefile example program : main.o iodat.o dorun.o g++ -o $@ main.o iodat.o dorun.o main.o : main.cc g++ -c $? iodat.o : iodat.cc g++ -c $? dorun.o : $? g++ -c dorun.cc
Macro (variable) assignments • You can use macros to represent text in a Makefile • Saves typing • Allows you to easily change the action of the Makefile • Assignment: MACRONAME = macro value • Usage: ${MACRONAME}
Simplified Example Makefile with macros OBJS = main.o iodat.o dorun.o CC = /usr/bin/g++ program : ${OBJS} ${CC} -o $@ ${OBJS} main.o : main.cpp ${CC} -c $? iodat.o : iodat.cpp ${CC} -c $? dorun.o : dorun.cpp ${CC} -c $?
Comments and other Makefile notes • Comments begin with a ‘#’ • Lines that are too long can be continued on the next line by placing a ‘\’ at the end of the first line
Invoking make • Be sure that your description file: • is called makefile or Makefile • is in the directory with the source files (to simplify) • make (builds the first target in the file) • make target(s) (builds target(s)) • Important options: • -n: don’t run the commands, just list them • -f file: use file instead of [Mm]akefile
Other useful Makefile tips • Include a way to clean up your mess: %make clean • No dependencies! clean: /bin/rm -f *.o core
Makefile example b : c d rm $@; echo $? > $@a : c rm $@; echo $? > $@target : a b rm $@; echo $? > $@; cat $? >> $@ all : a b c d target start : rm `ls | egrep –v ‘\<(M|m)akefile\>’` ; echo D > d ; echo C > c echo B > b ; echo A > a ; echo Target > target