140 likes | 263 Views
C Review. (language, compilation and debugging). Keith 9/22/2010. Content. C language overview Compilation debugging. Basic Data type. Integer int, short, long, long long and unsigned Char a byte has the same number of bits of char _Bool Integer with 1 bit in memory Floating point
E N D
C Review (language, compilation and debugging) Keith 9/22/2010
Content • C language overview • Compilation • debugging
Basic Data type • Integer • int, short, long, long long and unsigned • Char • a byte has the same number of bits of char • _Bool • Integer with 1 bit in memory • Floating point • float, double, and long double
Other data type • Pointer • type *p; • store the address of memory location • array • type name[dim]; • struct • struct ex{ int a; char b;}; • const • int * const p; /*a const pointer to a int*/ • const int * p; /*a pointer to a const int*/
statement • conditionals • if • switch-case • loops • while (do-while) • while(1){} • for • break/continue • change the flow of execution
functions • functions written by yourself • return-type function-name ( argument-list-if-necessary ){...local-declarations......statements...return return-value;} • library functions • include header files in your code • libraries • stdio.h • string.h • stdlib.h
preprocessing directives • #define Defines a macro. • #include Inserts text from another source file. • #ifdef Conditionally includes source text if a macro name is defined. • #ifndef Conditionally includes source text if a macro name is not defined. • #ifndef FILENAME_H #define FILENAME_H … #endif • #else Conditionally includes source text if the previous #ifdef, #ifndef test fails. • #endif Ends conditional text.
I/O capability • FILE *fp; • fp = fopen(name, mode); • fclose(fp); • fprintf(fp, "format string", variable list); • fgets(str_name, length, fp); • feof(fp); • while(!feof(fp)) { fgets(line, max_length, fp); … }
Command-line arguments • main(int argc, char** argv) • a.out -i 2 -g -x 3 • argc = 6argv[0] = "a.out"argv[1] = "-i"argv[2] = "2"argv[3] = "-g"argv[4] = "-x"argv[5] = "3"
Compilation • gcc codename –o programname • make prog = cc –o prog prog.c • options • -o filename • -g produce extra debugging information • -O(0/1/2/3) optimization level • -I(i)directory add header file directory • -l(L)libraryname link the library
Makefile • Multiple files compilation • files • main.c • factorial.c • hello.c • Run • make #Makefile_example all: hello hello: main.o factorial.o hello.o gcc main.o factorial.o hello.o -o hello main.o: main.c gcc -c main.c factorial.o: factorial.c gcc -c factorial.c hello.o: hello.c gcc -c hello.c clean: rm -rf *o hello
Another makefile CC=gcc CFLAGS=-c -Wall SOURCES=main.c hello.c factorial.c OBJECTS=$(SOURCES:.c=.o) EXECUTABLE=hello all: $(SOURCES) $(EXECUTABLE) $(EXECUTABLE): $(OBJECTS) $(CC) $(OBJECTS) -o $@ .c.o: $(CC) $(CFLAGS) $< -o $@ • http://mrbook.org/tutorials/make/
gdb for debugging • -g in compilation to enable gdb • gdb obj //start gdb with obj program • (gdb) run (arg <arguments>) • (gdb) s for step execution • (gdb) p for print variables • (gdb) br to set breakpoint • (gdb) set to modify registers or memory • set $eax=10 • (gdb) help
For more information • http://www.ic.sunysb.edu/Stu/xdeng/c.htm • Online documents