Presentation is loading. Please wait.

Presentation is loading. Please wait.

the c language BY SA 1972 by Ken Thompson and Dennis Ritchie.

Similar presentations


Presentation on theme: "the c language BY SA 1972 by Ken Thompson and Dennis Ritchie."— Presentation transcript:

1 http://proglit.com/

2 the c language

3 BY SA

4

5 1972 by Ken Thompson and Dennis Ritchie

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

7 http://proglit.com/

8 imperative/procedural statically typed weakly typed

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

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

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

12 http://proglit.com/

13 basic data types char 1-byte signed integer int n-byte signed integer float single-precision floating- point double double-precision floating-point

14 declarations type name; int monkey; float zebra;

15 functions returnType name(parameters) {body}

16 int square(int n) { return n * n; } int cube(int n) { return square(n) * n; }

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

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

19 http://proglit.com/

20 !0// 1 !1// 0 !0.6// 0 !3// 0 !-76.5// 0

21 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 }

22

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

24 http://proglit.com/

25 value variables float a = 9.3;

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

27 reference operator &lvalue int i; int *p; p = &i; char c; int *p; p = &c; // error

28 reference operator &lvalue int i; int *p; p = &i;

29 dereference operator *pointer int i = 3; int *p; p = &i; i = *p + 2;

30 int i; int *p; p = &i; *p = 6;

31 http://proglit.com/

32 pointer arithmetic int i; int *p; p = &i + 2; i = *p;

33 can subtract a pointer from a pointer can’t add a pointer to a pointer

34 char *p; p = (char *) 0xB000FFFF; char c = *p;

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

36 pointer comparisons == != > < >= <= !

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

38 http://proglit.com/

39 memory allocation malloc free

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

41 sizeof operator sizeof type sizeof int sizeof double sizeof(float *)

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

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

44 http://proglit.com/

45 array (a stack-allocated contiguous block of memory) type name[size]; float jack[8]; char jill[200];

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

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

48 int a[30]; … int sumA = sum(a, 30); int *b = malloc(20 * sizeof(int)); … int sumB = sum(b, 20);

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

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

51 http://proglit.com/

52 strings char *s = “abc”; char b = *(s + 1); int i = strlen(s);

53 structure (a programmer-defined compound data type) struct typeName {members}; struct typeName name; instance.member

54 struct cat { char *name; int age; }; // semi-colon! struct cat mittens; mittens.name = “Mittens”; mittens.age = 5;

55 struct cat mittens; struct cat fluffy; … mittens = fluffy; struct cat *p = &fluffy; mittens == fluffy // illegal

56 struct cat cats[8]; (*cats).name = “Oscar”; (*(cats + 1)).name = “Kitty”;

57 struct cat *cats = malloc(8 * (sizeof struct cat));

58 http://proglit.com/


Download ppt "the c language BY SA 1972 by Ken Thompson and Dennis Ritchie."

Similar presentations


Ads by Google