http://proglit.com/. the c language by sa 1972 by ken thompson and dennis ritchie
TRANSCRIPT
![Page 1: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/1.jpg)
http://proglit.com/
![Page 2: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/2.jpg)
the clanguage
![Page 3: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/3.jpg)
BY
SA
![Page 4: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/4.jpg)
![Page 5: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/5.jpg)
1972 by Ken Thompson and Dennis Ritchie
![Page 6: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/6.jpg)
• developed concurrently with Unix• C++ and Objective-C in 1980’s• ANSI C89, C90, and C99
• GCC (Gnu Compiler Collection)• MSVC (Microsoft Visual C++)
![Page 7: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/7.jpg)
http://proglit.com/
![Page 8: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/8.jpg)
• imperative/procedural• statically typed• weakly typed
![Page 9: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/9.jpg)
control of memory• explicitly manipulate individual bytes
• “portable assembly”• compact data
• manual allocation
![Page 10: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/10.jpg)
calling convention
• assembly from C• C from assembly
(how functions are called in machine code)
![Page 11: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/11.jpg)
systems programming(operating systems, device drivers)
performance-sensitive code
(games, media players)
![Page 12: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/12.jpg)
http://proglit.com/
![Page 13: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/13.jpg)
basic data typeschar 1-byte signed integerint n-byte signed integerfloat single-precision floating-pointdouble double-precision floating-point
![Page 14: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/14.jpg)
declarationstype name;
int monkey;float zebra;
![Page 15: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/15.jpg)
functions
returnType name(parameters) {body}
![Page 16: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/16.jpg)
int square(int n){ return n * n;}
int cube(int n){ return square(n) * n;}
![Page 17: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/17.jpg)
char foo(int a, float b, float c){ …}
![Page 18: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/18.jpg)
casting(type) expression
int x = 35;foo((char) x);
![Page 19: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/19.jpg)
http://proglit.com/
![Page 20: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/20.jpg)
!0 // 1!1 // 0!0.6 // 0!3 // 0!-76.5 // 0
![Page 21: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/21.jpg)
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}
![Page 22: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/22.jpg)
void roger(int x){ int z = 9; if (x == 3) { foo(z); } else { bar(z); } ack(z);}
![Page 23: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/23.jpg)
int main(){ printf(“Hello, world!”); return 0;}
![Page 24: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/24.jpg)
http://proglit.com/
![Page 25: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/25.jpg)
value variables
float a = 9.3;
![Page 26: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/26.jpg)
pointers(a data type representing an address)
int *foo;double *bar;char *ack;
![Page 27: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/27.jpg)
reference operator&lvalue
int i;int *p;p = &i;
char c;int *p;p = &c; // error
![Page 28: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/28.jpg)
reference operator&lvalue
int i;int *p;p = &i;
char c;int *p;p = (int *) &c;
![Page 29: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/29.jpg)
dereference operator
*pointer
int i = 3;int *p;p = &i;i = *p + 2;
![Page 30: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/30.jpg)
int i;int *p;p = &i;*p = 6;
![Page 31: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/31.jpg)
http://proglit.com/
![Page 32: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/32.jpg)
pointer arithmeticint i;int *p;p = &i + 2;i = *p;
![Page 33: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/33.jpg)
can subtract a pointer from a pointercan’t add a pointer to a pointer
![Page 34: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/34.jpg)
char *p;p = (char *) 0xB000FFFF;char c = *p;
![Page 35: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/35.jpg)
int *p;p = 0;if (p) { … } // false
![Page 36: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/36.jpg)
pointer comparisons== !=> <>= <=!
![Page 37: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/37.jpg)
weak typing(can violate data types)
float f;f = 98.6;f = f – 2;char *p;p = (char *)&f + 2;*p = 1;
![Page 38: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/38.jpg)
http://proglit.com/
![Page 39: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/39.jpg)
memory allocation
• malloc• free
![Page 40: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/40.jpg)
void *p;p = malloc(5);float *f;f = malloc(13);…free(p);free(f);
![Page 41: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/41.jpg)
sizeof operatorsizeof type
sizeof intsizeof doublesizeof(float *)
![Page 42: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/42.jpg)
int *p;p = malloc(7 * sizeof(int));*(p + 6) = 35;…free(p);
![Page 43: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/43.jpg)
int *p;p = malloc(7 * (sizeof int));if (p == 0) {
… // allocation failed} else {
… // allocation succeeded
free(p);}
![Page 44: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/44.jpg)
http://proglit.com/
![Page 45: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/45.jpg)
array(a stack-allocated contiguous block of memory)
type name[size];
float jack[8];char jill[200];
![Page 46: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/46.jpg)
int i[32];char c[11];int *p;p = i;*i = -6;*(i + 1) = 8;*(c + 7) = (char) 5;
![Page 47: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/47.jpg)
int sum(int *arr, int n){
int i = 0;int sum = 0;while (i < n) {
sum = *(arr + i) + sum;i = i + 1;
}return sum;
}
![Page 48: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/48.jpg)
int a[30];…int sumA = sum(a, 30);
int *b = malloc(20 * sizeof(int));…int sumB = sum(b, 20);
![Page 49: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/49.jpg)
char *fred(){
char c[30];return c;
}
![Page 50: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/50.jpg)
char *fred(){
char *c = malloc(30 * sizeof(char));
return c;}
char *foo = fred();…free(foo);
![Page 51: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/51.jpg)
http://proglit.com/
![Page 52: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/52.jpg)
stringschar *s = “abc”;char b = *(s + 1);int i = strlen(s);
![Page 53: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/53.jpg)
structure(a programmer-defined compound data type)
struct typeName {members};
struct typeName name;instance.member
![Page 54: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/54.jpg)
struct cat {char *name;int age;
}; // semi-colon!
struct cat mittens;mittens.name = “Mittens”;mittens.age = 5;
![Page 55: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/55.jpg)
struct cat mittens;struct cat fluffy;…mittens = fluffy;
struct cat *p = &fluffy;
mittens == fluffy // illegal
![Page 56: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/56.jpg)
struct cat cats[8];
(*cats).name = “Oscar”;(*(cats + 1)).name = “Kitty”;
![Page 57: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/57.jpg)
struct cat *cats = malloc(8 * (sizeof struct
cat));
![Page 58: Http://proglit.com/. the c language BY SA 1972 by Ken Thompson and Dennis Ritchie](https://reader035.vdocument.in/reader035/viewer/2022081512/551a9add550346b52d8b62ca/html5/thumbnails/58.jpg)
http://proglit.com/