1 / 58

http:// proglit.com /

http:// proglit.com /. the c language. SA. BY. 1972 by Ken Thompson and Dennis Ritchie. developed concurrently with Unix C++ and Objective-C in 1980’s ANSI C89, C90, and C99 GCC (Gnu Compiler Collection) MSVC (Microsoft Visual C++). http:// proglit.com /. imperative/procedural

bert
Download Presentation

http:// proglit.com /

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. http://proglit.com/

  2. the c language

  3. SA BY

  4. 1972 by Ken Thompson and Dennis Ritchie

  5. developed concurrently with Unix C++ and Objective-C in 1980’s ANSI C89, C90, and C99 GCC (Gnu Compiler Collection) MSVC (Microsoft Visual C++)

  6. http://proglit.com/

  7. imperative/procedural • statically typed • weakly typed

  8. control of memory explicitly manipulate individual bytes “portable assembly” compact data manual allocation

  9. (how functions are called in machine code) calling convention assembly from C C from assembly

  10. systems programming performance-sensitive code (operating systems, device drivers) (games, media players)

  11. http://proglit.com/

  12. basic data types char 1-byte signed integer intn-byte signed integer float single-precision floating-point double double-precision floating-point

  13. declarations typename; int monkey; float zebra;

  14. functions returnTypename(parameters) {body}

  15. int square(int n) { return n * n; } int cube(int n) { return square(n) * n; }

  16. char foo(int a, float b, float c) { … }

  17. casting (type) expression int x = 35; foo((char) x);

  18. http://proglit.com/

  19. !0 // 1 !1 // 0 !0.6 // 0 !3 // 0 !-76.5 // 0

  20. void roger(int x) { if (x == 3) { int z = 9; foo(z); } else { bar(z); // z does not exist } ack(z); // z does not exist }

  21. void roger(int x) { int z = 9; if (x == 3) { foo(z); } else { bar(z); } ack(z); }

  22. int main() { printf(“Hello, world!”); return 0; }

  23. http://proglit.com/

  24. value variables • float a = 9.3;

  25. pointers (a data type representing an address) int*foo; double*bar; char*ack;

  26. reference operator &lvalue char c; int*p; p = &c; // error inti; int*p; p = &i;

  27. reference operator &lvalue char c; int*p; p = (int*) &c; inti; int*p; p = &i;

  28. dereference operator *pointer inti = 3; int*p; p = &i; i = *p + 2;

  29. int i; int*p; p = &i; *p = 6;

  30. http://proglit.com/

  31. pointer arithmetic inti; int*p; p = &i + 2; i = *p;

  32. can subtracta pointer from a pointer can’t adda pointer to a pointer

  33. char*p; p = (char*) 0xB000FFFF; char c = *p;

  34. int*p; p = 0; if (p) { … } // false

  35. pointer comparisons • == != • > < • >= <= • !

  36. (can violate data types) weak typing float f; f = 98.6; f = f – 2; char*p; p = (char*)&f + 2; *p = 1;

  37. http://proglit.com/

  38. memory allocation malloc free

  39. void*p; p = malloc(5); float*f; f = malloc(13); … free(p); free(f);

  40. sizeof operator sizeoftype sizeof int sizeof double sizeof(float *)

  41. int*p; p = malloc(7 * sizeof(int)); *(p + 6) = 35; … free(p);

  42. int*p; p = malloc(7 * (sizeofint)); if (p == 0) { … // allocation failed } else { … // allocation succeeded free(p); }

  43. http://proglit.com/

  44. (a stack-allocated contiguous block of memory) array typename[size]; float jack[8]; char jill[200];

  45. inti[32]; char c[11]; int *p; p = i; *i = -6; *(i + 1) = 8; *(c + 7) = (char) 5;

  46. int sum(int *arr, int n) { int i = 0; int sum = 0; while (i < n) { sum = *(arr + i) + sum; i = i + 1; } return sum; }

  47. int a[30]; … intsumA = sum(a, 30); • int *b = malloc(20 * sizeof(int)); • … • intsumB = sum(b, 20);

  48. char *fred() { char c[30]; return c; }

  49. char *fred() { • char*c = malloc(30 * sizeof(char)); return c; } char*foo = fred(); … free(foo);

More Related