DOC PREVIEW
Rose-Hulman CSSE 332 - Study notes

This preview shows page 1-2-3-4-5 out of 16 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1CSSE 332Standard Library, Storage classes, and Make<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 automatically23Input 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.4Other useful 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 tasksFinding 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/nullStorage 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 auto6More 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 fields7More 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 file89enum - 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.10Program 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.h11To compile (by hand)gcc main.c mypgm.c –o runorgcc -c main.c -o main.ogcc -c mypgm.c -o mypgm.ogcc mypgm.o main.o -o runIn-class exercise Copy the makefile tutorial directory to your working environment, e.g.– scp -r [email protected]:/class/csse/csse332/0708c/Code/maketutorial temp– svn export temp maketutorial– rm –rf temp12In-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 again13To compile (using a makefile) Run make (woohoo!)14In-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 well1516Tips 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


View Full Document

Rose-Hulman CSSE 332 - Study notes

Download Study notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Study notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Study notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?