CSSE 332 Standard Library, Storage classes, and Make 1
<stdio.h> Provides a simple and efficient buffered I/O interface from man stdio Prototypes standard I/O functions (Mostly) system-independent (e.g. fprintf, fgets, feof) Defines required types (e.g. FILE) Defines required macros Functions are defined in standard library System-dependent implementations Linked automatically 2
Input and Output functions fgets(stdin, ); Similar to scanf( ) Safe from buffer overflow fprintf(stdout, format, ); Buffered output Equivalent to printf( format, ) fprintf(stderr,, ); Un-buffered output Use for error messages. 3
Other useful libraries and <string.h> header files String processing functions -- str*() & mem*() <ctype.h> Functions for testing characters <math.h> Mathematical functions and macros <stdlib.h> Functions for number conversion, storage allocation, and similar tasks 4
Finding the right library In bash: for x in /usr/lib/*.a; do if ar t $x grep pow &> /dev/null; then echo $x; fi; done 2> /dev/null
Storage classes Visibility: parts of source code within which variable can be referenced Lifetime: phases of execution during which variable is allocated Automatic (default for local variables) e.g. auto int i; Visibility: within statement block Lifetime: execution of statement block Register e.g. register counter = 1; Suggestion to compiler to place variable in a register Visibility and lifetime same as auto 6
More storage classes static e.g. static int i; Visibility: statement block Lifetime: program execution Value is retained and space is de-allocated only when program (not function) terminates. Historically related to Java s static fields 7
More storage classes extern e.g. int count; //declared in file 1 Visibility must be entire file (i.e. global) extern int count; //used in files 2,3 Promises compiler that variable will be visible from another compilation unit Provides type Common practice is to collect externs in a header file 8
enum - enumerated data types #include <stdio.h> enum month{ JANUARY, /* like #define JANUARY 0 */ FEBRUARY, /* like #define FEBRUARY 1 */ MARCH /* */ }; //In main: enum month birthmonth; if(birthmonth == JANUARY){ } /* alternatively,. */ enum month{ JANUARY=1, /* like #define JANUARY 1 */ MARCH=3, /* like #define MARCH 3 */ FEBRUARY=2, /* */ }; Note: if you use the #define, the value of JANUARY will not be visible in 9 the debugger. An enumerated data type s value will be.
Program with multiple files #include <stdio.h> #include mypgm.h int gdata=5; int main(){ myproc(); return 0; } main.c Library headers Standard User-defined #include <stdio.h> #include mypgm.h void myproc(){ int mydata = gdata*2;... /* some code */ } mypgm.c void myproc(); extern int gdata; mypgm.h 10
To compile (by hand) gcc main.c mypgm.c o run or gcc -c main.c -o main.o gcc -c mypgm.c -o mypgm.o gcc mypgm.o main.o -o run 11
In-class exercise Copy the makefile tutorial directory to your working environment, e.g. scp -r merkle@addiator.rosehulman.edu:/class/csse/csse332/0708c/code/ maketutorial temp svn export temp maketutorial rm rf temp 12
In-class exercise (cont.) Compile main.c and author.c Link them Make a change to author.c and build the executable again Make a change to author.h and build the executable again 13
To compile (using a makefile) Run make (woohoo!) 14
In-class exercise (cont.) Run make Make a change to author.c and run make again Make a change to author.h and run make again Examine the makefile Read the comments Note the variables Identify and understand the dependency rules (targets, dependencies, actions) Note on whitespace: Targets must be followed by tabs, not spaces Actions must be preceded by tabs, not spaces This makefile is more complicated than it needs to be, but scales well 15
Tips to programming well in C Always initialize your variables before using their values (especially pointers) Don t use pointers after freeing them Don t return pointers to local variables of functions There are no exceptions, so check for errors everywhere An array is also a pointer, but its value is immutable. Compile your programs with a Makefile 16