Copyright 1995 D. Hanson, K. Li & J.P. Singh Computer Science 217: ANSI C Programming Language Page 14 September 7, 1997 ANSI C Programming Language • A small, general-purpose, initially systems programming language • Used for writing the UNIX OS and tools for many computers • Now also very popular for general-purpose computing • A “low-level” languagedatatypes and control structures are close to those on most machines • Notable featurespointer (address) arithmetic and operatorsall functions are call-by-valuesimple, 2-level scope structureno I/O or memory management facilities (provided by library routines)“flexible” type structure • HistoryBCPL B C K&R C ANSI C~1960 ~1970 ~1972 ~1978 ~1988Copyright 1995 D. Hanson, K. Li & J.P. Singh Computer Science 217: C vs Modula-3 Page 15 September 7, 1997 C vs Modula-3 feature CModula-3 safe no yes efficient yes yes garbage collection no yes static typechecking mostly yes enforced interfaces no yes concurrency no yes Huh?widely available yes noeveryone knows it yes nosoftware tools yes somegood for a summer job yes noCopyright 1995 D. Hanson, K. Li & J.P. Singh Computer Science 217: C Program Structure Page 16 September 7, 1997 C Program Structure • Programs are composed of one or more files each file contains global variables and functions int a, b; /* global variables */int main(int argc, char *argv[]) {hello();return 0;}void hello(void) {printf("hello world\n");} • Execution begins by calling main ends when main returns (or some function calls the library function exit )Copyright 1995 D. Hanson, K. Li & J.P. Singh Computer Science 217: Function Definitions Page 17 September 7, 1997 Function Definitions • General form of an ANSI C function definition [ type ] name ( argument - declarations ) { body } int twice (int x, double y) {...} • If no return value, type of function should be void . • return statements specify function return values int twice(int x, double y) {return 2*x + y;} • Unlike in Pascal, functions are never defined within functionsCopyright 1995 D. Hanson, K. Li & J.P. Singh Computer Science 217: Declarations & Definitions Page 18 September 7, 1997 Declarations & Definitions • Declaration : specifies (announces) the properties of an identifier extern int sp;extern int stack[]; specify that “ sp is an int ” and “ stack is an array of ints ” extern indicates they are defined elsewhere - outside this routine, or even outside this file • Definition : declares the identifier and causes storage to be allocated int sp;int ptr = 1;;int stack[100]; declares sp , ptr and stack, allocates storage, ptr is initialized to 1 • Why does a language have declarations for variables? • Can a variable have multiple declarations?Copyright 1995 D. Hanson, K. Li & J.P. Singh Computer Science 217: Scope Page 19 September 7, 1997 Scope • How do functions defined in different files communicate? - by calling one another (parameter passing and return values- through global (externally declared) variables • External variables Externally declared versus extern ?Can we have multiple declarations of an externally defined variable within a file?What if an external declaration is not initialized? Is it treated as defined? • So which functions and data may a function reference? - determined by the scope of identifiersCopyright 1995 D. Hanson, K. Li & J.P. Singh Computer Science 217: Global Variables & Scope Page 20 September 7, 1997 Global Variables & Scope • The scope of an identifier says where the identifier can be used • Functions can use global variables declared outside and above them file a.c : int stack[100];main() {...}int sp;void push(int x) {...} • Global variables and functions in other files are made avaiilable with extern file b.c : extern int stack[];void dump(void) { ... } stack defined in a.c is visible here stack is visible stack, sp are visibleCopyright 1995 D. Hanson, K. Li & J.P. Singh Computer Science 217: Scope, cont’d Page 21 September 7, 1997 Scope, cont’d • Formal parameter and local declarations “hide” outer-level declarations int x, y;...f(int x, int a) {int b;...y = x + a*b;if (...) {int a;...y = x + a*b;}} • f(int x) {int x;...} struct a {int a;float b;} *f;float a = 1;typdef int a;int a(void){char *a;{double a;...}}Copyright 1995 D. Hanson, K. Li & J.P. Singh Computer Science 217: Scope, cont’d Page 22 September 7, 1997 Scope, cont’d • Formal parameter and local declarations “hide” outer-level declarations int x, y;...f(int x, int a) {int b;...y = x + a*b;if (...) {int a;...y = x + a*b;}} • Cannot declare the same variable name twice in one scope • f(int x) {int x;...} • Different name spaces allow same identifier to be multiply declared in a scope - function and typdef names; labels; struct/union tags; struct/union members formal parameter x hides global x local a hides formal parameter a error!Copyright 1995 D. Hanson, K. Li & J.P. Singh Computer Science 217: Function Arguments and Local Variables Page 23 September 7, 1997 Function Arguments and Local Variables • Local variables are temporary variables (unless declared static) created upon entry to the function in which they are declared destroyed upon return • Arguments are transmitted by value the values of the arguments are copied into “local variables” • Arguments are initialized local variables int a, b;main(void) {a = 1; b = 2;f(a);print(a, b);} output: 3 43 21 5 void f(int a) {a = 3;{int b = 4;print(a, b);}print(a, b);b = 5;}Copyright 1995 D. Hanson, K. Li & J.P. Singh Computer Science 217: Function Declarations Page 24 September 7, 1997 Function Declarations • Declares the type of the value returned and the types of arguments extern int f(int, float);extern int f(int a, float b); • A void function is a procedure • A void argument list means no arguments void hello(void) • Unlike Pascal, functions can be used before they are declaredas long as defined in same file or declared extern • A function without a declaration assumes the function returns an int assumes arguments have the types of the corresponding expressions “i = f(2.0, 1);” implies “int f(double, int);” if f is defined otherwise, anything goes!Copyright 1995 D. Hanson, K. Li & J.P. Singh Computer Science 217: Static Variables Page 25
View Full Document