Download presentation
Presentation is loading. Please wait.
1
1 CSSE 332 Standard Library, Storage classes, and Make
2
<stdio.h> “Provides a simple and efficient buffered I/O interface” – from man stdio “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” Functions are defined in “standard library” –System-dependent implementations –Linked automatically 2
3
3 Input and Output functions fgets(stdin,…); fgets(stdin,…); –Similar to scanf(…) –Safe from buffer overflow fprintf(stdout,”format”,…); fprintf(stdout,”format”,…); –Buffered output –Equivalent to printf(“format”,…) fprintf(stderr,”…”,…); fprintf(stderr,”…”,…); –Un-buffered output –Use for error messages.
4
4 Other useful libraries and header files –String processing functions -- str*() & mem*() –Functions for testing characters –Mathematical functions and macros –Functions for number conversion, storage allocation, and similar tasks
5
Finding the right library In bash: 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
6
Storage classes Visibility: parts of source code within which variable can be referenced Visibility: parts of source code within which variable can be referenced Lifetime: phases of execution during which variable is allocated Lifetime: phases of execution during which variable is allocated Automatic (default for local variables) Automatic (default for local variables) –e.g. auto int i; –Visibility: within statement block –Lifetime: execution of statement block Register Register –e.g. register counter = 1; –Suggestion to compiler to place variable in a register –Visibility and lifetime same as auto 6
7
More storage classes – static e.g. static int i; e.g. static int i; Visibility: statement block Visibility: statement block Lifetime: program execution Lifetime: program execution Value is retained and space is de-allocated only when program (not function) terminates. Value is retained and space is de-allocated only when program (not function) terminates. Historically related to Java’s static fields Historically related to Java’s static fields 7
8
More storage classes – extern e.g. 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 Promises compiler that variable will be visible from another compilation unit Provides type Provides type Common practice is to collect externs in a header file Common practice is to collect externs in a header file 8
9
9 enum - enumerated data types #include 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 the debugger. An enumerated data type’s value will be.
10
10 Program with multiple files Library headers Library headers –Standard –User-defined void myproc(); extern int gData; #include #include “mypgm.h” void myproc(){ int mydata = gData*2;... /* some code */ } #include #include “mypgm.h” int gData=5; int main(){ myproc(); return 0; } main.c mypgm.c mypgm.h
11
11 To compile (by hand) gcc main.c mypgm.c –o run or or gcc -c main.c -o main.o gcc -c main.c -o main.o gcc -c mypgm.c -o mypgm.o gcc -c mypgm.c -o mypgm.o gcc mypgm.o main.o -o run gcc mypgm.o main.o -o run
12
In-class exercise Copy the makefile tutorial directory to your working environment, e.g. Copy the makefile tutorial directory to your working environment, e.g. –scp -r merkle@addiator.rose- hulman.edu:/class/csse/csse332/0708c/Code/ maketutorial temp –svn export temp maketutorial –rm –rf temp 12
13
In-class exercise (cont.) Compile main.c and author.c Compile main.c and author.c Link them Link them Make a change to author.c and build the executable again Make a change to author.c and build the executable again Make a change to author.h and build the executable again Make a change to author.h and build the executable again 13
14
To compile (using a makefile) Run make (woohoo!) Run make (woohoo!) 14
15
In-class exercise (cont.) Run make Run make Make a change to author.c and run make again Make a change to author.c and run make again Make a change to author.h and run make again Make a change to author.h and run make again Examine the makefile 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
16
16 Tips to programming well in C Always initialize your variables before using their values (especially pointers) Always initialize your variables before using their values (especially pointers) Don’t use pointers after freeing them Don’t use pointers after freeing them Don’t return pointers to local variables of functions Don’t return pointers to local variables of functions There are no “exceptions,” so check for errors everywhere There are no “exceptions,” so check for errors everywhere An array is also a pointer, but its value is immutable. An array is also a pointer, but its value is immutable. Compile your programs with a Makefile Compile your programs with a Makefile
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.