1 / 14

(language, compilation and debugging)

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

finnea
Download Presentation

(language, compilation and debugging)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C Review (language, compilation and debugging) Keith 9/22/2010

  2. Content • C language overview • Compilation • debugging

  3. 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

  4. 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*/

  5. statement • conditionals • if • switch-case • loops • while (do-while) • while(1){} • for • break/continue • change the flow of execution

  6. 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

  7. 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.

  8. 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); … }

  9. 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"

  10. 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

  11. 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

  12. 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/

  13. 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

  14. For more information • http://www.ic.sunysb.edu/Stu/xdeng/c.htm • Online documents

More Related