1 / 25

Debugging Techniques and String Operations in C Programming

Learn about debugging with gdb, using macros with #define, managing strings, and common C functions for string manipulation. Improve code quality and efficiency with these essential concepts.

Download Presentation

Debugging Techniques and String Operations in C Programming

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. Lecture 5 CIS 208 January 28th, 2005

  2. Homework 2 • Questions? • Complaints? • Queries?

  3. Debugger • Text version is gdb • Installed on all ‘nix machines • compile with the –g option (inserts db info) • Set break points • Monitor Variable values.

  4. DDD • Graphical debugger. • Installed on most ‘nix machines • If remotely accessing from windows box, need to turn on X-server. • X-server allows remote graphics.

  5. #define #include <stdio.h> #define PI 3.1415 int main(void) { printf(“PI = %f\n”,PI); return 0; }

  6. #define • Preprocessor command • Not a variable • Either literal or macro • No semicolon

  7. #define preprocessor • Value plugged in #define HI “Hello world\n” int main(void) { printf(HI); return 0; } #define HI “Hello World\n” int main(void) { printf(“HI”); return 0; }

  8. Defines to defines #define ONE 1 #define TWO ONE+ONE #define THREE ONE+TWO

  9. more #define #define TRUE 1 #define FALSE 0 int main(void) { if (TRUE) {. . . }

  10. Function Macros #define ABS(a) (a)<0 ? –(a) : (a) • Macro is placed in code int main(void) { int j = ABS(-3); return 0; } int main(void) { int j = (-3)<0 ? –(a) : (a); return 0; }

  11. Why use #define • Changing magic numbers • #define A_SIZE 100 • Increases Readability • Macros faster than Functions. • Increase code size vs. memory use.

  12. Quick Array review • Size? • The Memory runneth over • Initialization

  13. Strings • Not Fundamental type • Not an object • Just an array of characters

  14. In memory “abcd” == {‘a’,’b’,’c’,’d’,’\0’} • Must have terminating character ‘\0’

  15. Static memory Strings • char ch[10] = “Hi there”; • char ch[] = “Hello”; • char ch[4] = “abcdef”; WRONG

  16. Reading and writing strings • Scanf and Printf work. • A bit clunky. • %s in scanf • Stops at first white space • “this is a test”

  17. getchar() <stdio.h> • Reads characters one at a time • Starts at newline • Still has buffering problem

  18. getchar() • returns a character • char ch = getchar(); • ch[0] = getchar();

  19. getchar() • Filling strings • Don’t overflow the size int main(void) { char ch[10]; int k = 0; while (k < 10 && (ch[k++] = getchar()) != ‘\n’); ch[k-1] = ‘\0’; printf(“%s\n”,ch); return 0; }

  20. getch() & getche() • work same as getchar() • No buffering problem • Read in as soon at key is pressed • Not in ANSI C • Not sure if in gcc.

  21. putchar(char) <stdio.h> • just writes a character to the screen. • Easy stuff. No formatting.

  22. puts(char[]) <stdio.h> • Prints a string starting at the input address • Writes until terminator • will auto newline after terminator

  23. gets(char[]) • reads in an entire string. • Terminates with newline • auto-adds terminating character

  24. gets • Giant buffering problem • Don’t ever use. • Don’t ever use.

  25. <string.h> • strcat • string concat. • strcmp • string compare • strcpy • string copy • strlen • string length

More Related