1 / 64

C Basics

C Basics. The C Language Spirit. Made by professional programmers for professional programmers Very flexible, very efficient, very liberal Does not protect the programmers from themselves.

nancystone
Download Presentation

C Basics

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

  2. The C Language Spirit • Made by professional programmers for professional programmers • Very flexible, very efficient, very liberal • Does not protect the programmers from themselves. • Rationale: programmers know what they are doing even UNIX and most “serious” system software (servers, compilers, etc) are written in C. • Can do everything Java and C++ can. It’ll just look uglier in C

  3. Compiler • gcc • See manual “man” for options • man gcc • Preprocessor • Compiler • Linker • C89 versus C99 • C99: Mix variable declarations and code • make – a compilation utility • Google for make files (or GNU Make)

  4. Programming in C C = Variables + Instructions

  5. Programming in C C = Variables + Instructions char int float pointer array string …

  6. Programming in C C = Variables + Instructions char assignment int printf/scanf float if pointer for array while string switch … …

  7. Variables Name 10,000 x Value1 10,002 y Value2 Memory Address Value 10,008 z Value3 10,010 p Value4 10,012 d Value5 …

  8. The “&” Operator:Reads “Address of” Name 10,000 &y x Value1 10,002 y Value2 Value 10,008 z Value3 10,010 p Value4 10,012 d Value5 …

  9. Pointers Name A pointer is a variable whose value is the address of another 10,000 x Value1 10,002 y Value2 Value 10,008 z Value3 10,010 p 10,002 10,012 d Value5 …

  10. The “*” OperatorReads “Variable pointed to by” Name A pointer is a variable whose value is the address of another 10,000 x Value1 10,002 y *P Value2 Value 10,008 z Value3 10,010 p 10,002 10,012 d Value5 …

  11. What is the Output? main() { int *p, q, x; x=10; p=&x; *p=x+1; q=x; printf (“Q = %d\n“, q); }

  12. What is the Output? main() { int *p, q, x; x=10; p=&x; *p=x+1; q=x; printf (“Q = %d\n“, q); } #@*%! p #@%$! q @*%^ x

  13. What is the Output? main() { int *p, q, x; x=10; p=&x; *p=x+1; q=x; printf (“Q = %d\n“, q); } #@*%! p #@%$! q 10 x

  14. What is the Output? main() { int *p, q, x; x=10; p=&x; *p=x+1; q=x; printf (“Q = %d\n“, q); } p #@%$! q 10 x

  15. What is the Output? main() { int *p, q, x; x=10; p=&x; *p=x+1; q=x; printf (“Q = %d\n“, q); } p #@%$! q 11 x

  16. What is the Output? main() { int *p, q, x; x=10; p=&x; *p=x+1; q=x; printf (“Q = %d\n“, q); } p 11 q 11 x

  17. Cardinal Rule: Must Initialize Pointers before Using them int *p; *p = 10; BAD

  18. Cardinal Rule: Must Initialize Pointers before Using them int *p; *p = 10; #@*%! p ?? Pointing somewhere random

  19. Cardinal Rule: Must Initialize Pointers before Using them int *p; *p = 10; #@*%! p 10 #@*%!

  20. How to Initialize Pointers

  21. How to Initialize Pointers • Set pointer equal to location of known variable int *p; int x; … p=&x;

  22. How to Initialize Pointers • Use malloc() int *p; … p=(int*) malloc (sizeof (int));

  23. How to Initialize Pointers • Create an Array int p[10]; Same as: int *p; p=(int*) malloc (10*sizeof (int));

  24. Arrays int p[5]; p p[0] p[1] Name of array (is a pointer) p[2] p[3] p[4] Shorthand: *(p+1) is called p[1] *(p+2) is called p[2] etc..

  25. Example int y[4]; y[1]=6; y[2]=2; y y[0] y[1] 6 y[2] 2 y[3]

  26. Array Name as Pointer • What’s the difference between the examples below: Example 1: int z[8]; int *q; q=z; Example 2: int z[8]; int *q; q=&z[0];

  27. Array Name as Pointer • What’s the difference between the examples below: Example 1: int z[8]; int *q; q=z; Example 2: int z[8]; int *q; q=&z[0]; NOTHING!! x (the array name) is a pointer to the beginning of the array, which is &x[0]

  28. Example: How much is y at the end: int y, x, *p; x = 20; *p = 10; y = x + *p;

  29. Example: How much is y at the end: int y, x, *p; x = 20; *p = 10; y = x + *p; BAD!! Dereferencing an unitialized pointer will likely segfault or overwrite something! Segfault = unauthorized memory access

  30. Question: • What’s the difference between int* q; int q[5]; • What’s wrong with: int ptr[2]; ptr[1] = 1; ptr[2] = 2;

  31. Question: • What is the value of b[2] at the end? int b[3]; int* q; b[0]=48; b[1]=113; b[2]=1; q=b; *(q+1)=2; b[2]=*b b[2]=b[2]+b[1];

  32. Strings (Null-terminated Arrays of Char) • Example char s[5]; • Strings are arrays that contain the string characters followed by a “Null” character to indicate end of string. • Do not forget to leave room for the null character s s[0] s[1] s[2] s[3] s[4]

  33. Conventions Strings • “string” • “c” Character • ‘c’

  34. String Operations • strcpy • strlen • srtcat • strcmp

  35. strcpy, strlen Syntax: strcpy(ptr1, ptr2);    where ptr1 and ptr2 are pointers to char value = strlen(ptr);    where value is an integer and    ptr is a pointer to char Example: int len; char str[15]; strcpy (str, "Hello, world!"); len = strlen(str);

  36. strcpy, strlen What’s wrong with char str[5]; strcpy (str, "Hello");

  37. strncpy Syntax: strncpy(ptr1, ptr2, num);    where ptr1 and ptr2 are pointers to char num is the number of characters to be copied Example: int len; char str1[15], str2[15]; strcpy (str1, "Hello, world!"); strncpy (str2, str1, 5);

  38. strncpy Syntax: strncpy(ptr1, ptr2, num);    where ptr1 and ptr2 are pointers to char num is the number of characters to be copied Example: int len; char str1[15], str2[15]; strcpy (str1, "Hello, world!"); strncpy (str2, str1, 5); Caution: strncpy blindly copies the characters. It does not voluntarily append the string-terminating null character.

  39. strcat • Syntax: strcat(ptr1, ptr2);    where ptr1 and ptr2 are pointers to char Concatenates the two null terminates strings yielding one string (pointed to by ptr1). char S[25] = "world!"; char D[25] = "Hello, "; strcat(D, S);

  40. strcatExample • What’s wrong with: char S[25] = "world!"; strcat(“Hello, ”, S);

  41. strcmp • Syntax: diff = strcmp(ptr1, ptr2);    where diff is an integer and     ptr1 and ptr2 are pointers to char • Returns zero if strings are identical int diff; char s1[25] = "pat"; char s2[25] = "pet"; diff = strcmp(s1, s2);

  42. Math: Increment and Decrement Operators Example 1: int x, y, z, w; y=10; w=2; x=++y; z=--w; Example 2: int x, y; y=10; w=2; x=y++; z=w--;

  43. Math: Increment and Decrement Operators Example 1: int x, y, z, w; y=10; w=2; x=++y; z=--w; Example 2: int x, y; y=10; w=2; x=y++; z=w--; First increment/decrement then assign result x is11, z is 1 First assign then increment/decrement x is 10, z is 2

  44. Math: Increment and Decrement Operators on Pointers Example 1: int a[2]; int number1, number2, *p; a[0]=1; a[1]=10; a[2]=100; p=a; number1 = *p++; number2 = *p; What will number1 and number2 be at the end?

  45. Math: Increment and Decrement Operators on Pointers Example 1: int a[2]; int number1, number2, *p; a[0]=1; a[1]=10; a[2]=100; p=a; number1 = *p++; number2 = *p; What will number1 and number2 be at the end? Hint: ++ increments pointer p not variable *p

  46. Logic: Relational (Condition) Operators == equal to != not equal to > greater than < less than >= greater than or equal to <= less than or equal to

  47. Logic Example if (a == b) printf (“Equal.”); else printf (“Not Equal.”); • Question: what will happen if I replaced the above with: if (a = b) printf (“Equal.”); else printf (“Not Equal.”);

  48. Correct C Programs

  49. The C compiler cc [filename.c] Options include: -o [output file name] -lm to include the maths library -E to get output from preprocessor only (no compilation). The compiler works in three stages: 1. it preprocesses (replacing the #include, #define etc); 2. it compiles the preprocessed files; 3. it links all of the pieces of code together into the executable program. For a large program you can compile a small piece at a time, and link it to larger, pre-compiled parts. To compile only, use: cc -c [filename.c]This creates object files (whose names end in .o), which can then be linked later by using them instead of the source files.

  50. ar= archive ruv= replace, update, verbose object files to go in the library g_lib.a= output archive file name C Compiler: Libraries It is possible to create libraries, which are (generally large) collections of useful object code ready for linking, so they don’t need to be compiled over and over again for different programs. To do this with a set of .o files, ar ruv g_lib.a gfopen.o gfclose.o gcalloc.o....... This makes a library file called g_lib.a. Follow this by ranlib g_lib.a which organizes the file in a form that is useful for the linker. To make the functions available to other programs, add the library tothe list of compiled files: cc -o pgm main.c file1.c g_lib.a

More Related