C Programming Day 4 based upon Practical C
C Programming Day 4 based upon Practical C Programming by Steve Oualline CS 550 Operating Systems
Tic Tac Toe Example • See class website – http: //catpages. nwmissouri. edu/m/monismi/cs 550/ examples. html • Compilation gcc Tic. Tac. Toe. c tttmain. c -ansi -o ttt. exe
Tic Tac Toe Makefile • Using a makefile for compilation – use make to compile • Makefiles are similar to a script to compile programs • We break many of our programs into groups of files – header files with function prototypes (. h files) –. c files with function definitions – file or files containing main function(s)
Example Makefile • Example with one. c file CC=gcc CFLAGS=-ansi prog 3 : prog 3. c $(CC) $(CFLAGS) -o prog 3. c #There must be a tab on the line following prog 3 #(the target) #Comments are preceded with a pound sign/hash #symbol (#)
More on Makefiles • If you name your makefile "makefile", then to compile you simply type the command make. • Otherwise, you need to specify the name of your makefile as follows: make -f my_makefile_name
Tic Tac Toe Makefile CC=gcc CFLAGS=-ansi #executable first, then sources ttt: Tic. Tac. Toe. o tttmain. o $(CC) $(CFLAGS) -o ttt Tic. Tac. Toe. o tttmain. o: Tic. Tac. Toe. h tttmain. c $(CC) $(CFLAGS) -c tttmain. c Tic. Tac. Toe. o: Tic. Tac. Toe. h Tic. Tac. Toe. c $(CC) $(CFLAGS) -c Tic. Tac. Toe. c
More on functions • • • void functions with parameters return types call by value call by reference
Call by value int min(int a, int b) { if(a < b) return a; return b; } The values of a and b are copied from those of the formal parameters.
Call by Reference Example for Arrays #include <stdio. h> //pass in the address of the array int length(char[]); //Call by reference void change. Char(char [], int, char); //Call by value void change. Int(int, int);
Example main Function int main(int argc, char ** argv){ char line[100]; while(1) { fgets(line, sizeof(line), stdin); //Read a line[strlen(line) - 1] = '