CS241Crash course in CCrash?Slide 5Slide 6Linkage & ScopeLinkageutil.cmain.cSlide 11Declarations to library functions?C Compilergcc Gnu CompilerC VariantsPointersSlide 17int *pSlide 19char **argvchar ***argvArgument ArraysCharacter stringsstrcmpWhy is this bad?Ans: Understand storageIdentifies & Linkage SummaryIdentifiersLinkage classesStorage classes: static and automaticCS241Introduction To CLawrence AngraveCrash course in CCrash char *p; p=0;*p=4?char * search(char*) {char result[100];… do stuffreturn result;}?static int debug=1;main(…){}?int counter() {static int c=1;printf(“The value of c is %x”,&c);return c++;}Linkage & ScopeLinkageIdentifiers (variables & functions) visible to another c file?Main.c = Compilation unitMyutils.c = Another compilation unitStatic : No!util.cstatic int notthisone;int runsuperfast=0;void die(char*) { static int privatecounter; fprintf(stderr,mesg); exit(1);}main.c Refer to runsuperfast and die?extern int runsuperfast;void die(char *mesg);main.c Refer to runsuperfast and die?void die(char *); // declaration!extern int runsuperfast;Declarations to library functions?#include <stdio.h>#include <stdlib.h>#include “myutils.h”C Compilergcc Gnu CompilerMany options!man gccThree for the price of onePreprocessorCompilerLinkerC VariantsC99 - A ‘nicer’ C/* Multi-line comments */// line comment (Not allowed in C89)Mix variable declarations & code inside functionsPointersPointersHow to shoot yourself in the foot and not realize itint *pQ. What is p?How much storage is required for p?Where is p in memory?int *pWhat is the value of p == &*p; p[0] == *pchar **argvExplain this to your neighborchar ***argvExplain this to your neighborWhen would you use it?Argument Arrays An argument array is an array of pointers terminated by a NULL pointer.Each element of the array is of type char * and represents a string.Character stringsBasic support in Compiler for string constants and string pointersEverything else is a library functionstrcmpstrncmpstrlenstrcatstrcmpstrcmp(“Hello”,”Hello”) returns what?strcmp(“1”,”2”) returns what?Why is this bad?char *p=“Hello”;strcpy(p,” World”);Ans: Understand storageIdentifies & Linkage SummaryIdentifiers“Identifier” denotes an object: a function, a tag, member of a structure, etc. We mainly consider identifiers associated with functions and variablesScope is a region of program text where identifier is used Identifiers declared more than once may refer to the same object because of linkageEach identifier has a linkage class of external, internal or none. An identifier representing an object has a linkage class related to storage class (storage duration).Linkage classes Linkage class determines whether variables can be accessed in files other than the one in which they are declared. Internal linkage class means they can only be accessed in the file in which they are declared. External linkage class means they can be accessed in other files. Variables declared outside any function and function name identifiers have external linkage by default.They can be given internal linkage with the key word static . Variables declared inside a function are only known inside that function and are said to have no linkage.Storage classes: static and automatic Static storage class refers to variables that, once allocated, persist throughout the execution of a program. Automatic storage class refers to variables which come into existence when the block in which they are declared is entered and are discarded when the defining block is exited. Variables declared inside a function have automatic storage class unless they are declared to be static.These are usually allocated on the program stack. Variables defined outside any functions have static storage class. The word static has two meanings is C.One is related to storage class and the other to linkage
View Full Document