CSSE 332Standard Library, Storage classes, and Make2Why learn C (after Java)? Both high-level and low-level language Better control of low-level mechanisms Performance better than Java Java hides many details needed for writing OS codeBut,…. Memory management responsibility Explicit initialization and error detection More room for mistakes3Goals of this tutorial To introduce some basic C concepts to you– so you can read further details and learn more C To warn you about common mistakes made by beginners– so that you get your assignments done quickly4The Standard library Not part of the C language But an environment that supports standard C provides: – Function declarations – Type definitions– Macro definitions of this library Functions, types and macros declared in standard header filesK & R pg 241Input / Output header file <stdio.h> Defines input and output functions, types, and macros:– file operations – formatted output – formatted input – character input and output functions – direct input and output functions – file positioning functions – error functions5K & R pg 2416Useful libraries and header files <string.h>– 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 tasks7Input and Output functions fscanf(stdin,…);– Equivalent to scanf(…) fprintf(stdout,”format”,…); – Buffered output– Equivalent to printf(“format”,…) fprintf(stderr,”…”,…); – Un-buffered output– Use for error messages.8Storage classes Automatic (default for local variables)– Allocate memory only when function is executed– e.g. auto int i; Register– Direct compiler to place variable in a register– e.g. register counter = 1; Static– Allocate memory as soon as program execution begins– Scope is local to the function that declares the variable.– Value is retained and space is de-allocated only when program (not function) quits.– e.g. static int i;9More storage classes Extern– For a variable shared by two or more files: int count; //global variable in file 1 extern int count; //used in files 2,3 … Exists as long as the program Can collect externs in a .h file– extern int count; /* does not create variable count or allocate storage for it. This just gives its attributes */ Says it’s an int Says it’s defined elsewhere10enum - 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 the debugger. An enumerated data type’s value will be.11Program with multiple files Library headers– Standard– User-definedvoid myproc();extern int gData;#include <stdio.h>#include “mypgm.h”void myproc(){int mydata = gData*2;. . . /* some code */}#include <stdio.h>#include “mypgm.h”int gData=5;int main(){ myproc();return 0;}main.cmypgm.cmypgm.h12Tips to program well in C Always initialize your declarations before using them (especially pointers) Don’t use pointers after freeing them Don’t return pointers to function’s local variables No exceptions – so check for errors everywhere An array is also a pointer, but its value is immutable. Compile your programs with a Makefile13To compile gcc main.c mypgm.c –o runorgcc -c main.c -o main.ogcc -c mypgm.c -o mypgm.ogcc mypgm.o main.o -o run Can also use a Makefile– A Makefile example– Checkout the maketutorial project to your csse332 directory and study the MakefileMakefiles Tutorial adapted from– http://www.eng.hawaii.edu/Tutor/Make/– http://www.cs.bgu.ac.il/~sadetsky/openu/myMakeTutorial.txt14Compiling a single program15gcc green.c blue.cORgcc –o program1 green.c blue.cCompiling multiple related files16More efficient compilationgcc –c green.c => create green.ogcc –c blue.c => create blue.ogcc green.o blue.o => link and create a.out The “c” flag compiles and creates object files while ignoring functions and variables defined externally.17 When you separate your C program into many files, keep these points in mind: – When you use a global variable, Define it in one source file– int global_variable; declare it in the header file– extern int global_variable; include the header file in the appropriate C source files– Write all function prototypes in header files and include the header files in the appropriate C source files.– Exactly one of the files must have a main() function.Splitting your C program18Target: Dependencies<tab> How to compileDependency graphs and Makefiles19Makefileall: project1project1: data.o main.o io.o<tab> gcc data.o main.o io.o –o project12021Makefile Create a file called Makefile. At the prompt, type “make”.22Make cleanclean:<tab>rm –f *.o <executable>At the prompt:make clean23To create more than one executable1. Include the following line in the Makefileall: <exe1> <exe2> ….2. Write the rest of the Makefile.3. At the prompt, type “make”.24To make more than one…. If you don’t want to re-create all the executables every timea. Specify only the ones that should be with “all”.ORb. Don’t use “all”. Instead, type:make <exe1> <exe2> ….25CC = gccDEBUG = -gLFLAGS = -Wall $(DEBUG) CFLAGS = -Wall –c $(DEBUG)main: green.o blue.o$(CC) $(LFLAGS) green.o blue.o –o maingreen.o: green.c common.h$(CC) $(CFLAGS) green.cblue.o: blue.c common.h$(CC) $(CFLAGS) blue.cMacros in Makefiles26References http://www.mtsu.edu/~csdept/FacilitiesAndResources/make.htm#What%20make http://www.eng.hawaii.edu/Tutor/Make/
View Full Document