CS120 Day 4: C Functions, Variables & Program Structure; makefiles ------------------------------------------------------------------- Function Definitions ================================== - can go anywhere with prototype - should appear before function call without prototype - use void ftype if no return value (return statement optional) - default return type is int (if none specified) - parameter list - type and local name of each variable - int is default type if none listed - (float x, y) vs. (float x, float y) - definitions cannot be nested! - all params are call by value (so not changed!) (vs. call by ref) Function Prototypes ====================================== - ftype fname (argtype1, argtype2, etc); - goes after include section, before main Storage classes/variable types =========================== - lifetime vs. scope - local vars - global (external) vars - static vars - static can be used with any type of variable declaration eg: static int intarray[10]; - means that the memory location exists for duration of program, but varname can only be used where it has local scope - contents of variable stay there even when not in local use - static vars are automatically initialized to 0 by compiler (not reinitialized each time function w/var starts) >>>> FIND ERRORS in variables1.c to show storage types <<<< >wget http://www.cs.jhu.edu/~joanne/cs120/notes/variables1.c >>>> TRACE variables.c (corrected version of variables1.c) in gdb <<<< Arrays and Functions ================================= - use [] to indicate array variable in function definition and in function prototype eg. void doArray (int [], int); /* prototype */ void doArray (int myarray[], int size) - pass whole array with just name, no subscript - array values are passed by REFERENCE - element values may change - doesn't create new storage location (ie, doesn't copy array) - array name is really address of first element - array names are passed by VALUE (copied, cannot be changed) - pass individual array element just like any other variable (by value) - can use const in parameter list to make array unmodifiable: void doArray (const int ma[]) results in compile error if any attempt to assign or read new value into an array element - multiple subscript arrays - two dimensional - declare as myarray[size1][size2], access myarray[i][j] - initialize by nesting lists, or not nesting - does initialization row by row - for passing to functions, need sizes of subs after the first eg: void passarray (int a[][3][4]) is 3 dimensional array because arrays are stored linearly - use dimensions to locate items Program Structure ===================================== - header files (*.h): contain function prototypes - function files (*.c): contain function definitions - compilation: - need to include all .c files for a project - prevent multiple inclusion of header files: #ifndef MYRAND_H #define MYRAND_H // prototypes go here #endif Random built-in functions ================================= - rand() - generates integer between 0 and RAND_MAX (>= short int) - #include - n = a + rand() % b; to get random int between a and a+b-1 - n = a + rand() % (b-a+1); to get random int between a & b inclusive - seeding the random generator: - srand(time(NULL)) to (skip while debugging) - #include Make our own random helper methods in stand-alone files: - see header file myRand.h - write implementation file myRand.c >wget http://www.cs.jhu.edu/~joanne/cs120/notes/myRand.h >wget http://www.cs.jhu.edu/~joanne/cs120/notes/testRand.c Makefiles ========================================== WHAT - files with compilation instructions for all files in a project - can specify compiler and flags to use - use to specify target files for compilation components - targets specify file dependencies - gives rules for assembly and linkage (optional) - can include a "clean" instruction to delete unnecessary files HOW - run with "make" command-line program - default filename is "makefile" or "Makefile" - can use -f flag to specify another filename: make -f MakeRand - can make specific components, such as cleaning up: make clean WHY - as you are developing a project, you can incrementally add files to make and using make will only recompile files changed since the last time it was used - can be used with other types of commands/languages too - see the man pages for more info - examples: myRand.h, myRand.c, testRand.c, MakeRand MORE UNIX TOOLS ============================================= - viewing files: cat, more, less - basic file commands: ls, mkdir, cd, cp, mv, rm - printing: lp filename (check what default printer is) - hidden files start with '.' - use ls -a to view them - compile & run >gcc file.c -> creates a.out -> run with ./a.out >gcc -o file file.c -> creates executable file -> run with ./file - zipping files together: - version 1: look up zip in the manual (> man zip) - version 2: (old school) we create a "tarball" and use the compression option (z) >tar zcvf pg1.tar.gz pg1 zips up all the files in directory pg1 >tar zxvf pg1.tar.gz extracts all the files in the zip - downloading a file from the web: wget http://someURL - several "shell" options: bash, tcsh, etc. - customize your environment with various login files depending on the shell you choose to use: .login, .cshrc, .bashrc, .tshrc, etc. - will automatically be run every time you open a new shell - need to set one up for your ugrad account, and for lubuntu On your unix ugrad account: >cp ~cssumr5/.cshrc .cshrc then change whatever you want to change. These are all basic unix commands that can be typed from the command-line too. You can look up how to use these various unix commands in the man pages. In Lubuntu it appears that you need to use = to define aliases: alias mygcc='gcc -Wall -Wextra -pedantic -std=c99 -g' Common uses/commands - can set prompt: set prompt="joanne> " - can set default editor: setenv EDITOR emacs - can create aliases: alias clear alias c clear alias e emacs alias ls 'ls -F' alias ugrad 'ssh -l joanne ugrad4.cs.jhu.edu' alias gcc120 'gcc -std=c99 -pedantic -Wall -Wextra -O' alias g++120 'g++ -std=c++98 -pedantic -Wall -Wextra -O'