gcc -o myExecutable myCode.c // compile (default program name is a.out) ./myExecutable // run int i = 3; double y = 3.3; long z = 123000; printf("%d %f %ld", x, y, z); printf("Storage size: %d %d %d", sizeof(x), sizeof(y), sizeof(z); An enumeration type has values limited to one of a specified set. enum color { red, green, yellow }; // defining an enum data type enum color chair; // declaring an enum variable chair = red; // using an enum in an expression /* a pointer is a special variable that holds a memory address */ int i = 3; int *p = &i; // p is a declared pointer printf("%d %d\n", i, *p); // 3 3 char c = 'c'; char *cp = &c; char s[10]; // s is a constant pointer s[0] = 'a'; // can't assign a whole string - only at initialization: char s[] = "data"; // declaration and initialization. s is length 5. // s is constant and can't be assigned a different array address char s[] = {'d','a','t','a','\0'}; // this is equivalent printf("%s %s %c", s, &s[1], s[1]); // data ata a char *q = &s; // q is not a constant pointer address q = "bird"; // assign to q the address of a string constant // & and * are the reverse of each other. // & is the address operator. &q[0] is the same as q // * is the dereferencing operator. *q is the same as q[0] printf("%s %s %s %c %c %c", q, q+1, &q[1], *q, *(q+1), q[1]); // bird ird ird b i i while(*q) // equivalent to while(*q != '\0') printf("%c\n", *q++); // increment address stored in a pointer strcpy(s, "chicken"); // strcpy copies each character until it reaches \0 strcpy(q, "chicken"); // BAD - copies to an un-allocated memory location // Pointers can be used to produce dynamically sized arrays: int *d = malloc(3*sizeof(int)); // now use d[0], d[1] and d[2] if((q = (char *)malloc(20 * sizeof(char))) == NULL) { printf("Out of memory\n"); exit(0); } // now we can use strcpy(q, "chicken") safely calloc(20, sizeof(char)) also clears all memory blocks free(q); // free memory that was assign with malloc or calloc char aa[2][6]; // two dimensional array - // array of 2 arrays of 5 characters each strcpy(aa[0], "cat"); // this is ok but there is unused memory strcpy(aa[1], "dog"); print_two_dim_array1(aa, 2); print_two_dim_array2((char *)aa, 2, 6); void print_two_dim_array1(char r[][100], int d1) { // 2nd dimension hard-coded int x; for (x=0; x a = 13; // assigning value to a struct element } // using a pointer to the struct struct strct st; st.a = 12; // assigning value to a struct element struct strct x = {3,'c'}; struct strct x = {3}; // x.b is initialized to 0 populate(&st); char c; char *q; gets(q); // read string from standard input (doesn't store \n) puts(q); // print string to standard output. Newline character is appended c = getchar(); // read single character from standard input c = getc(); // as above ungetc(c, fp); // unget last character (can replace it with a different character) putchar(c); // print character to standard output int var; scanf("%d", &var); // read int from standard input printf("%d", var); char str[20]; scanf("%s", str); // read string from standard input prinf("%s", str); fscanf(stdin, "%s%d", s, &i); // read from keyboard fprintf(stdout, "%s %d", s, i); // write to screen sprintf(q, "%s %d", s, i); // write to string int i; char s1[5], s2[5]; sscanf("one 2 three", "%s %s", s1, s2); // read from string i = atoi(s2); printf("%s %d \n", s1, i); FILE *fp; if((fp=fopen("test","r")) == NULL) { // open file for read perror("Error message is"); exit(1); } c = fgetc(fp); // read single char from file while ((c = fgetc(fp)) != EOF) { // read from file character by character printf ("%c", c); } fgets(q, 20, fp); // read 20 characters or until encounters EOF or \n // stores \n too, unlike gets() fputs("hello world", fp); // write string to file fputc('p', fp); // write single char to file putc('p', fp); // as above fread(q, itemSize, nItems, fp); // read itemSize*nItems bytes from stream fwrite(q, itemSize, nItems, fp); // write itemSize*nItems bytes from stream fprintf(fp, "%s - %d", s, i); // write to file fscanf(fp, "%s%d", s, &i); // read from file rewind(fp); // reset file pointer to beginning of file ftell(fp); // return current file offset fseek(fp, offset, whence); // re-position fp by offset according to whence The new position, measured in bytes from the beginning of the file, shall be obtained by adding offset to the position specified by whence. The specified point is the beginning of the file for SEEK_SET, the current value of the file-position indicator for SEEK_CUR, or end-of-file for SEEK_END. fclose(fp); i = system("date"); // Execute shell command #include // for file operations #include #include // for perror() /*************************************************************************/ STRING MANIPULATION #include char c, *s1,*s2, *s3; int i; s3 = strcat(s1, s2); // concatenate two strings s3 = strncat(s1, s2, n);// concatenate two strings. At most n characters are appended i = strcmp(s1, s2); // compare two strings; return 0 if strings are equal i = strncmp(s1, s2, n); // compare first n characters of two strings strcpy(s1, s2); // copy string s2 to s1, stopping at \0 of s1 strncpy(s1, s2); // copy string up to n characters. may overwrite \0 of s1 i = strlen(s1); // length of string i = strchr(s1, c); // search string for first occurence of character c i = strrchr(s1, c); // search string for last occurence of character c strtok(s1, delimeter); // return individual tokens from string // On first call pass the string, on subsequent calls pass a NULL to get rest of tokens: char *str = "Where is my bacon, dude?"; char *token; if ((token = strtok(str, ".,?! ")) != NULL) { do { printf("Word: \"%s\"\n", token); } while ((token = strtok(NULL, ".,?! ")) != NULL); } /*************************************************************************/ PREPROCESSOR #define ABC 100 // Substitutes every occurrence of ABC with 100 #undef ABC // Cancels previous #define for identifier ABC #define F(x) ((x)+1) // Macro y = 2 * F(j) // substituted with y = 2 * ((j)+1) #include // Replaces this line with contents of file math. the brackets <> specify that math.h should be found in a "standard" directory defined in the compiler configuration. Does not search directory of the source file. #include "ABC" // ABC is sought in the directory of the source file. If not found there, then it is sought along the search path specified by the -l option. If not found there, "standard directoriesre searched. #if ABC + 3 // True if ABC+3 evaluates to non-zero #ifdef ABC // True if ABC is currently defined by #define #ifndef ABC // True if ABC is not currently defined by #define #else #endif /*************************************************************************/ /* demo.c */ # include #include "add.h" int main( int argc, char* argv[] ) { int result; { result= add(1, 2); printf( "result is %d\n", result ); return 0; } /* add.c */ #include #include "add.h" int add( int n1, int n2 ) { return n1 + n2; } /* add.h */ int add( int, int ); gcc assumes C style linking g++ assumes C++ style linking file are processed through one or more of four stages: preprocessing, compilation, assembly, and linking gcc -c add.c demo.c gcc add.o demo.o -o demo demo # demo.make # demo: demo.o add.o # dependency target: component gcc demo.o add.o -o demo # rule rule demo.o: demo.c add.h # make facility looks at file's time stamp gcc -c demo.c # the rule is a sequence of shell commands add.o: add.c add.h # -c means don't make executable - just oblect file gcc -c add.c # -o precedes the specified name of the executable make # The make utility automate the process of determining which # modules need to be recompiled due to their dependency relationships. make -f MyMakefile # if there's more than one makefile The dependencies in the makefile have the form: target: Components rule The first line is called a dependency and the second line is called a rule. The first character on a rule line must be the TAB character. One or more rule lines may follow a dependency. The order of dependency specifications is important # demo.make # CC = gcc # macro definition CFLAGS = -c demo : demo.o add.o $(CC) demo.o add.o -o demo demo.o : demo.c add.h $(CC) $(CFLAGS) demo.c add.o : add.c add.h $(CC) $(CFLAGS) add.c Dynamically maintained macros: $* The basename of the current target $< The name of the current dependency file. $@ The name of the current target $? The names of prerequisites that are younger that the target $% Evaluated only when the current target is an archive library member of the form libname (member.o). When this is the case, $@ evaluates to libname and $% evaluates to member.o. .c.o : # double suffix rule: update a target with a suffix of .o $(CC) -c $< # from prerequisite with a suffix of .c .o : # single suffix rule: create executable for each .o $(CC) -o $@ $< clean : # run with: make clean -f demo.make -rm *.o The archive Unix utility (ar) helps to organize and group object modules. ar -r math.a mathOpers.o fact.o # create archive names math.a ar -t math.a # list archive content ar -d math.a fact.o # delete a file from archive gcc -o demo demo.o math.a # static linking - The linker includes the code of math.o in the executable gcc -shared -o mathlib.so add.o # create a shared library mathlib.so # dynamic linking - When a program is linked to a library, # the actual code of the library routines is not included in the executable. # This reduces its size. gdb programName # run debugger. compile program with the flag -g first for better results.