SlideShare a Scribd company logo
Brief Introduction to the C Programming Language Fred Kuhns [email_address] Applied Research Laboratory, Department of Computer Science and Engineering, Washington University in St. Louis
Introduction ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Standard C ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Elements of a C Program ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
A Simple C Program ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],/*  you generally want to * include stdio.h and * stdlib.h *  */ #include <stdio.h> #include <stdlib.h> int main (void) { printf (“Hello World  ”); exit(0); }
Source and Header files ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Another Example C Program example.c /*  this is a C-style comment  * You generally want to palce * all file includes at start of file *  */ #include <stdio.h> #include <stdlib.h> int main (int argc, char **argv) { // this is a C++-style comment // printf prototype in stdio.h printf(“Hello, Prog name = %s”, argv[0]); exit(0); } /*  comments  */ #ifndef _STDIO_H #define _STDIO_H ... definitions and protoypes #endif /usr/include/stdio.h /*  prevents including file * contents multiple * times  */ #ifndef _STDLIB_H #define _STDLIB_H ... definitions and protoypes #endif /usr/include/stdlib.h #include  directs the preprocessor to “include” the contents of the file at this point in the source file. #define   directs preprocessor to define macros.
Passing Command Line Arguments ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],./try –g 2 fred argc  = 4, argv  =  <address0> ‘ t’‘r’‘y’‘’ argv : [0]  <addres1> [1]  <addres2> [2]   <addres3> [3]   <addres4> [4]  NULL ‘ -’‘g’‘’ ‘ 2’‘’ ‘ f’‘r’‘e’‘d’‘’
C Standard Header Files you may want to use ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
The Preprocessor ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Preprocessor: Macros ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Preprocessor: Conditional Compilation ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Another Simple C Program ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Arrays and Pointers 0 1 2 3 4 1 0 2 3 little endian byte ordering memory layout for array x
Pointers ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Pointers in C (and C++) Address 0x3dc 0x3d8 Program Memory 0x3cc 0x3c8 0x3c4 0x3c0 Note: The compiler converts z[1] or *(z+1) to Value at address  ( Address of z   +  sizeof(int)) ; In C you would write the byte address as: (char *)z + sizeof(int); or letting the compiler do the work for you (int *)z + 1; Step 1 : int main (int argc, argv) { int  x  = 4; int * y  = & x ; int * z [4] = { NULL ,  NULL ,  NULL ,  NULL }; int  a [4] = {1, 2, 3, 4}; ... 0x3bc 0x3b8 0x3b4 0x3b0 0x3d4 0x3d0 z[3] z[2] z[1] z[0] a[3] a[2] a[1] a[0] x y 4 0x3dc 0 0 0 0 4 3 2 1 NA NA
Pointers Continued 4 0x3dc Address 0x3dc 0x3d8 Program Memory 0x3bc 0x3b8 0x3b4 0x3b0 0x3cc 0x3c8 0x3c4 0x3c0 Step 1: int main (int argc, argv) { int  x = 4; int *y = &x; int *z[4] = {NULL, NULL, NULL, NULL}; int  a[4] = {1, 2, 3, 4}; Step 2 : Assign addresses to array Z z [0] =  a;   // same as & a [0] ; z [1] =  a + 1;   // same as &a[1] ; z [2] = a + 2; // same as &a[2]; z [3] = a + 3; // same as &a[3]; 0x3bc 0x3b8 0x3b4 0x3b0 4 3 2 1 NA 0x3d4 0x3d0 z[3] z[2] z[1] z[0] a[3] a[2] a[1] a[0] NA x y
Pointers Continued 4 0x3dc Address 0x3dc 0x3d8 Program Memory 0x3bc 0x3b8 0x3b4 0x3b0 0x3cc 0x3c8 0x3c4 0x3c0 Step 1: int main (int argc, argv) { int x = 4; int *y = &x; int *z[4] = {NULL, NULL, NULL, NULL}; int a[4] = {1, 2, 3, 4}; Step 2: z [0] = a; z [1] = a + 1; z [2] = a + 2; z [3] = a + 3; Step 3 : No change in z’s values z[0] = (int *)((char *)a); z[1] = (int *)((char *)a  + sizeof(int)); z[2] = (int *)((char *)a + 2 * sizeof(int)); z[3] = (int *)((char *)a + 3 * sizeof(int)); 0x3bc 0x3b8 0x3b4 0x3b0 4 3 2 1 NA 0x3d4 0x3d0 z[3] z[2] z[1] z[0] a[3] a[2] a[1] a[0] NA x y
Getting Fancy with Macros ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],# define   QINSERT_BEFORE (loc, node, field) do { *(loc)->field.prev = (node); (node)->field.prev = (loc)->field.prev; (loc)->field.prev  =  &((node)->field.next); (node)->field.next = (loc); } while (/* */0) # define   QINSERT_AFTER (loc, node, field) do { ((loc)->field.next)->field.prev =  &(node)->field.next; (node)->field.next = (loc)->field.next; (loc)->field.next = (node);  (node)->field.prev = &(loc)->field.next; } while ( /* */ 0) # define   QREMOVE (node, field) do { *((node)->field.prev) = (node)->field.next; ((node)->field.next)->field.prev = (node)->field.prev; (node)->field.next = (node); (node)->field.prev = &((node)->field.next); } while ( /* */ 0)
After Preprocessing and Compiling typedef struct wth_t { int state; QNODE(wth_t) alist; } wth_t; # define  QNODE (type) struct { struct type *next; struct type **prev; } typedef struct wth_t { int state; struct { struct wth_t *next; struct wth_t **prev; } alist; } wth_t; < integer >  state < address > next < address > prev 3 words in memory # define   QNODE_INIT (node, field) do {   (node)->field.next = (node);  (node)->field.prev = &(node)->field.next;} while ( /* */ 0 ); 0 0x00100 0x00104 0x100 head : instance of wth_t 0x104 0x108 memory layout after GCC CPP QNODE_INIT(head, alist)
QNODE Manipulations # define   QINSERT_BEFORE ( head ,  node ,  alist )do { *( head )-> alist .prev = ( node );  ( node )-> alist .prev = ( head )-> alist .prev;  ( head )-> alist .prev  = &( node )-> alist. next;( node )-> alist .next = ( head ); } while (/* */0) QINSERT_BEFORE(head, node0, alist); ? before 0x100 0 0x100 0x104 head  0x104 0x108 0x1a0 0 0x1a0 0x1a4 node0  0x1a4 0x1a8
QNODE Manipulations # define   QINSERT_BEFORE ( head ,  node ,  alist )do { *( head )-> alist .prev = ( node );  (node)->alist.prev = (head)->alist.prev;  (head)->alist.prev  = &(node)->alist.next;(node)->alist.next = (head); } while (/* */0) QINSERT_BEFORE(head, node0, alist); before 0x100 0 0x100 0x104 head  0x104 0x108 0x1a0 0 0x1a0 0x1a4 node0  0x1a4 0x1a8 0x100 0 0x1a0 0x104 head   0x104 0x108 0x1a0 0 0x1a0 0x1a4 node0   0x1a4 0x1a8
QNODE Manipulations # define   QINSERT_BEFORE ( head ,  node ,  alist )do { *( head )-> alist .prev = ( node );  ( node )-> alist .prev = ( head )-> alist .prev;  ( head )-> alist .prev  = &( node )-> alist. next;( node )-> alist .next = ( head ); } while (/* */0) QINSERT_BEFORE(head, node0, alist); before 0x100 0 0x100 0x104 head  0x104 0x108 0x1a0 0 0x1a0 0x1a4 node0  0x1a4 0x1a8 0x100 0 0x1a0 0x104 head   0x104 0x108 0x1a0 0 0x1a0 0x104 node0   0x1a4 0x1a8
QNODE Manipulations # define   QINSERT_BEFORE ( head ,  node ,  alist )do { *( head )-> alist .prev = ( node );  ( node )-> alist .prev = ( head )-> alist .prev;  ( head )-> alist .prev  = &( node )-> alist. next;( node )-> alist .next = ( head ); } while (/* */0) QINSERT_BEFORE(head, node0, alist); before 0x100 0 0x100 0x104 head  0x104 0x108 0x1a0 0 0x1a0 0x1a4 node0  0x1a4 0x1a8 0x100 0 0x1a0 0x1a4 head   0x104 0x108 0x1a0 0 0x1a0 0x104 node0   0x1a4 0x1a8
QNODE Manipulations # define   QINSERT_BEFORE ( head ,  node ,  alist )do { *( head )-> alist .prev = ( node );  ( node )-> alist .prev = ( head )-> alist .prev;  ( head )-> alist .prev  = &( node )-> alist. next;( node )-> alist .next = ( head ); } while (/* */0) QINSERT_BEFORE(head, node0, alist); before 0x100 0 0x100 0x104 head  0x104 0x108 0x1a0 0 0x1a0 0x1a4 node0  0x1a4 0x1a8 0x100 0 0x1a0 0x1a4 head   0x104 0x108 0x1a0 0 0x100 0x104 node0   0x1a4 0x1a8
QNODE Manipulations # define   QINSERT_BEFORE ( head ,  node ,  alist )do { *( head )-> alist .prev = ( node );  ( node )-> alist .prev = ( head )-> alist .prev;  ( head )-> alist .prev  = &( node )-> alist. next;( node )-> alist .next = ( head ); } while (/* */0) QINSERT_BEFORE(head, node0, alist); before 0x100 0 0x100 0x104 head  0x104 0x108 0x1a0 0 0x1a0 0x1a4 node0  0x1a4 0x1a8 0x100 0 0x1a0 0x1a4 head   0x104 0x108 0x1a0 0 0x100 0x104 node0   0x1a4 0x1a8
Adding a Third Node QINSERT_BEFORE(head, node1, alist); # define   QINSERT_BEFORE ( head ,  node ,  alist )do { *( head )-> alist .prev = ( node );    ( node )-> alist .prev = ( head )-> alist .prev;    ( head )-> alist .prev  = &( node )-> alist. next;   ( node )-> alist .next = ( head ); } while (/* */0) 0x100 0 0x1a0 0x1a4 head  0x104 0x108 0x1a0 0 0x100 0x104 node0  0x1a4 0x1a8 0x200 0 0x200 0x204 node1 0x204 0x208 0x200 0 0x200 0x204 node1 0x204 0x208 0x100 0 0x1a0 0x1a4 head   0x104 0x108 0x1a0 0 0x100 0x104 node0   0x1a4 0x1a8
Adding a Third Node QINSERT_BEFORE(head, node1, alist); # define   QINSERT_BEFORE ( head ,  node1 ,  alist )do { *( head )-> alist .prev = ( node1 );    ( node1 )-> alist .prev = ( head )-> alist .prev;    ( head )-> alist .prev  = &( node1 )-> alist. next;   ( node1 )-> alist .next = ( head ); } while (/* */0) (1) (1) 0x100 0 0x1a0 0x1a4 head  0x104 0x108 0x1a0 0 0x100 0x104 node0  0x1a4 0x1a8 0x100 0 0x1a0 0x1a4 head  0x104 0x108 0x1a0 0 0x200 0x104 node0  0x1a4 0x1a8 0x200 0 0x200 0x204 node1 0x204 0x208 0x200 0 0x200 0x204 node1 0x204 0x208
Adding a Third Node QINSERT_BEFORE(head, node1, alist); # define   QINSERT_BEFORE ( head ,  node1 ,  alist )do { *( head )-> alist .prev = ( node1 );    ( node1 )-> alist .prev = ( head )-> alist .prev;    ( head )-> alist .prev  = &( node1 )-> alist. next;   ( node1 )-> alist .next = ( head ); } while (/* */0) (1) (2) (2) 0x100 0 0x1a0 0x1a4 head  0x104 0x108 0x1a0 0 0x100 0x104 node0  0x1a4 0x1a8 0x100 0 0x1a0 0x1a4 head  0x104 0x108 0x1a0 0 0x200 0x104 node0  0x1a4 0x1a8 0x200 0 0x200 0x204 node1 0x204 0x208 0x200 0 0x200 0x1a4 node1 0x204 0x208
Adding a Third Node QINSERT_BEFORE(head, node1, alist); # define   QINSERT_BEFORE ( head ,  node1 ,  alist )do { *( head )-> alist .prev = ( node1 );    ( node1 )-> alist .prev = ( head )-> alist .prev;    ( head )-> alist .prev  = &( node1 )-> alist. next;   ( node1 )-> alist .next = ( head ); } while (/* */0) (3) (3) 0x100 0 0x1a0 0x1a4 head  0x104 0x108 0x1a0 0 0x100 0x104 node0  0x1a4 0x1a8 0x100 0 0x1a0 0x204 head  0x104 0x108 0x1a0 0 0x200 0x104 node0  0x1a4 0x1a8 0x200 0 0x200 0x204 node1 0x204 0x208 0x200 0 0x200 0x1a4 node1 0x204 0x208 (1) (1) (2) (2)
Adding a Third Node QINSERT_BEFORE(head, node1, alist); # define   QINSERT_BEFORE ( head ,  node1 ,  alist )do { *( head )-> alist .prev = ( node1 );    ( node1 )-> alist .prev = ( head )-> alist .prev;    ( head )-> alist .prev  = &( node1 )-> alist. next;   ( node1 )-> alist .next = ( head ); } while (/* */0) (1) (1) (2) (2) (3) (3) (4) (4) 0x100 0 0x1a0 0x1a4 head  0x104 0x108 0x1a0 0 0x100 0x104 node0  0x1a4 0x1a8 0x100 0 0x1a0 0x204 head  0x104 0x108 0x1a0 0 0x200 0x104 node0  0x1a4 0x1a8 0x200 0 0x200 0x204 node1 0x204 0x208 0x200 0 0x100 0x1a4 node1 0x204 0x208
Removing a Node QREMOVE(node0, alist); # define   QREMOVE ( node ,  alist ) do { (1) *(( node )-> alist .prev) = ( node )-> alist .next; (2) (( node )-> alist .next)-> alist .prev = ( node )-> alist .prev;(3) ( node )-> alist .next = ( node ); (4) ( node )-> alist .prev = &(( node )-> alist .next); } while ( /* */ 0) 0x100 0 0x1a0 0x204 head  0x104 0x108 0x1a0 0 0x200 0x104 node0  0x1a4 0x1a8 0x200 0 0x100 0x1a4 node1 0x204 0x208 0x100 0 ?? ?? head  0x104 0x108 0x1a0 0 ?? ?? node0  0x1a4 0x1a8 0x200 0 ?? ?? node1 0x204 0x208
Removing a Node QREMOVE(node0, alist); # define   QREMOVE ( node ,  alist ) do { *(( node )-> alist .prev) = ( node )-> alist .next;   (( node )-> alist .next)-> alist .prev = ( node )-> alist .prev;  ( node )-> alist .next = ( node );   ( node )-> alist .prev = &(( node )-> alist .next); } while ( /* */ 0) 0x100 0 0x1a0 0x204 head  0x104 0x108 0x1a0 0 0x200 0x104 node0  0x1a4 0x1a8 0x200 0 0x100 0x1a4 node1 0x204 0x208 0x100 0 0x1a0 0x204 head  0x104 0x108 0x1a0 0 0x200 0x104 node0  0x1a4 0x1a8 0x200 0 0x100 0x1a4 node1 0x204 0x208
Removing a Node QREMOVE(node0, alist); # define   QREMOVE ( node0 ,  alist ) do { (1) *(( node0 )->alist.prev) = ( node0 )->alist.next;   (( node0 )->alist.next)->alist.prev = ( node0 )->alist.prev;  ( node0 )->alist.next = ( node0 );   ( node0 )->alist.prev = &(( node0 )->alist.next); } while ( /* */ 0) (1) 0x100 0 0x200 0x204 head  0x104 0x108 0x1a0 0 0x200 0x104 node0  0x1a4 0x1a8 0x200 0 0x100 0x1a4 node1 0x204 0x208 0x100 0 0x1a0 0x204 head  0x104 0x108 0x1a0 0 0x200 0x104 node0  0x1a4 0x1a8 0x200 0 0x100 0x1a4 node1 0x204 0x208
Removing a Node QREMOVE(node0, alist); # define   QREMOVE ( node0 ,  alist ) do { *(( node0 )->alist.prev) = ( node0 )->alist.next; (2)  (( node0 )->alist.next)->alist.prev = ( node0 )->alist.prev;( node0 )->alist.next = ( node0 ); ( node0 )->alist.prev = &(( node0 )->alist.next); } while ( /* */ 0) (2) 0x100 0 0x200 0x204 head  0x104 0x108 0x1a0 0 0x200 0x104 node0  0x1a4 0x1a8 0x200 0 0x100 0x104 node1 0x204 0x208 0x100 0 0x1a0 0x204 head  0x104 0x108 0x1a0 0 0x200 0x104 node0  0x1a4 0x1a8 0x200 0 0x100 0x1a4 node1 0x204 0x208
Removing a Node QREMOVE(node0, alist); # define   QREMOVE ( node0 ,  alist ) do { *(( node0 )->alist.prev) = ( node0 )->alist.next;   (( node0 )->alist.next)->alist.prev = ( node0 )->alist.prev;(3)  ( node0 )->alist.next = ( node0 ); ( node0 )->alist.prev = &(( node0 )->alist.next); } while ( /* */ 0) (3) 0x100 0 0x200 0x204 head  0x104 0x108 0x1a0 0 0x1a0 0x104 node0  0x1a4 0x1a8 0x200 0 0x100 0x104 node1 0x204 0x208 0x100 0 0x1a0 0x204 head  0x104 0x108 0x1a0 0 0x200 0x104 node0  0x1a4 0x1a8 0x200 0 0x100 0x1a4 node1 0x204 0x208
Removing a Node QREMOVE(node0, alist); # define   QREMOVE ( node0 ,  alist ) do { *(( node0 )->alist.prev) = ( node0 )->alist.next;   (( node0 )->alist.next)->alist.prev = ( node0 )->alist.prev;( node0 )->alist.next = ( node0 );   (4)  ( node0 )->alist.prev = &(( node0 )->alist.next); } while ( /* */ 0) (4) 0x100 0 0x200 0x204 head  0x104 0x108 0x1a0 0 0x1a0 0x1a4 node0  0x1a4 0x1a8 0x200 0 0x100 0x104 node1 0x204 0x208 0x100 0 0x1a0 0x204 head  0x104 0x108 0x1a0 0 0x200 0x104 node0  0x1a4 0x1a8 0x200 0 0x100 0x1a4 node1 0x204 0x208
Solution to Removing a Node QREMOVE(node0, alist); # define   QREMOVE ( node ,  alist ) do { (1) *(( node )-> alist .prev) = ( node )-> alist .next; (2) (( node )-> alist .next)-> alist .prev = ( node )-> alist .prev;(3) ( node )-> alist .next = ( node ); (4) ( node )-> alist .prev = &(( node )-> alist .next); } while ( /* */ 0) 0x100 0 0x1a0 0x204 head  0x104 0x108 0x1a0 0 0x200 0x104 node0  0x1a4 0x1a8 0x200 0 0x100 0x1a4 node1 0x204 0x208 0x100 0 0x200 0x204 head  0x104 0x108 0x1a0 0 0x1a0 0x1a4 node0  0x1a4 0x1a8 0x200 0 0x100 0x104 node1 0x204 0x208
Functions ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Basic Types and Operators ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Operator Precedence  (from “C a Reference Manual”, 5 th  Edition) 15 16 Precedence right-to-left unary address of & right-to-left unary indirection ( dereference ) * right-to-left unary negation, plus - + right-to-left unary logical not ! right-to-left unary bitwise not ~ right-to-left prefix increment, decrement ++  -- left-to-right postfix compound literal ( type ){ init } left-to-right postfix increment, decrement ++ -- left-to-right postfix direct selection . left-to-right postfix function call f (...) left-to-right postfix subscripting a [k] size indirect selection simple tokens Operator n/a primary names, literals right-to-left unary sizeof left to right postfix -> Associates Class Tokens 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Precedence right-to-left unary casts ( type ) left-to-right binary multiplicative * / %  left-to-right binary additive + - sequential eval. assignment conditional logical or logical and bitwise or bitwise xor bitwise and equality/ineq. relational left, right shift Operator left-to-right binary , right-to-left binary = += -= *= /= %= &= ^= |= <<= >>= right-to-left ternary ?: left-to-right binary || left-to-right binary && left-to-right binary | left-to-right binary ^ left-to-right binary & left-to-right binary == != left-to-right binary < <= > >= left-to-right binary << >> Associates Class Tokens
Structs and Unions ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Conditional Statements (if/else) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Conditional Statements (switch) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Loops ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Building your program ,[object Object],[object Object],[object Object],[object Object],[object Object]
make and Makefiles, Overview ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Makefiles ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Example Makefile for wulib ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],# Makefile.inc # Contains common definitions MyOS := $(shell uname -s) MyID := $(shell whoami) MyHost := $(shell hostname) WARNSTRICT := -W   -Wstrict-prototypes -Wmissing-prototypes WARNLIGHT := -Wall WARN := ${WARNLIGHT} ALLFLGS := -D_GNU_SOURCE -D_REENTRANT -D_THREAD_SAFE APPCFLGS = $(ALLFLGS) $(WARN) WUCC := gcc WUCFLAGS := -DMyOS=$(MyOS) $(OSFLAGS) $(ALLFLGS) $(WARN) WUINCLUDES := WULIBS := -lm ifeq (${MyOS), SunOS) OSLIBS+= -lrt endif Makefile.inc Makefile
Project Documentation ,[object Object],[object Object],[object Object],[object Object],[object Object]
Attacking a Project ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

More Related Content

What's hot

Presentation 5th
Presentation 5thPresentation 5th
Presentation 5th
Connex
 
Declarative Type System Specification with Statix
Declarative Type System Specification with StatixDeclarative Type System Specification with Statix
Declarative Type System Specification with Statix
Eelco Visser
 

What's hot (20)

OpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ ProgrammingOpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ Programming
 
C++ Presentation
C++ PresentationC++ Presentation
C++ Presentation
 
C Programming Tutorial - www.infomtec.com
C Programming Tutorial - www.infomtec.comC Programming Tutorial - www.infomtec.com
C Programming Tutorial - www.infomtec.com
 
Python Programming
Python ProgrammingPython Programming
Python Programming
 
TDD in C - Recently Used List Kata
TDD in C - Recently Used List KataTDD in C - Recently Used List Kata
TDD in C - Recently Used List Kata
 
Summary of C++17 features
Summary of C++17 featuresSummary of C++17 features
Summary of C++17 features
 
C++ references
C++ referencesC++ references
C++ references
 
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
 
Hooking signals and dumping the callstack
Hooking signals and dumping the callstackHooking signals and dumping the callstack
Hooking signals and dumping the callstack
 
File Handling in C Programming
File Handling in C ProgrammingFile Handling in C Programming
File Handling in C Programming
 
C++17 std::filesystem - Overview
C++17 std::filesystem - OverviewC++17 std::filesystem - Overview
C++17 std::filesystem - Overview
 
C++ theory
C++ theoryC++ theory
C++ theory
 
CS4200 2019 | Lecture 4 | Syntactic Services
CS4200 2019 | Lecture 4 | Syntactic ServicesCS4200 2019 | Lecture 4 | Syntactic Services
CS4200 2019 | Lecture 4 | Syntactic Services
 
Constructor,destructors cpp
Constructor,destructors cppConstructor,destructors cpp
Constructor,destructors cpp
 
Tutorial on c language programming
Tutorial on c language programmingTutorial on c language programming
Tutorial on c language programming
 
Presentation 5th
Presentation 5thPresentation 5th
Presentation 5th
 
Declarative Type System Specification with Statix
Declarative Type System Specification with StatixDeclarative Type System Specification with Statix
Declarative Type System Specification with Statix
 
Notes part 8
Notes part 8Notes part 8
Notes part 8
 
C Theory
C TheoryC Theory
C Theory
 
STL ALGORITHMS
STL ALGORITHMSSTL ALGORITHMS
STL ALGORITHMS
 

Viewers also liked

Viewers also liked (9)

5 intro to networking
5 intro to networking5 intro to networking
5 intro to networking
 
C tutorial
C tutorialC tutorial
C tutorial
 
Mysql2
Mysql2Mysql2
Mysql2
 
Itmg360 chapter one_v05
Itmg360 chapter one_v05Itmg360 chapter one_v05
Itmg360 chapter one_v05
 
Lab 4 excel basics
Lab 4 excel basicsLab 4 excel basics
Lab 4 excel basics
 
Basic networking hardware pre final 1
Basic networking hardware pre final 1Basic networking hardware pre final 1
Basic networking hardware pre final 1
 
Basic c operators
Basic c operatorsBasic c operators
Basic c operators
 
Slides 2-basic sql
Slides 2-basic sqlSlides 2-basic sql
Slides 2-basic sql
 
Sql ppt
Sql pptSql ppt
Sql ppt
 

Similar to C

Brief introduction to the c programming language
Brief introduction to the c programming languageBrief introduction to the c programming language
Brief introduction to the c programming language
Kumar Gaurav
 
Functions And Header Files In C++ | Bjarne stroustrup
Functions And Header Files In C++ | Bjarne stroustrupFunctions And Header Files In C++ | Bjarne stroustrup
Functions And Header Files In C++ | Bjarne stroustrup
SyedHaroonShah4
 
Getting Started Cpp
Getting Started CppGetting Started Cpp
Getting Started Cpp
Long Cao
 
C programming session 01
C programming session 01C programming session 01
C programming session 01
Dushmanta Nath
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
Alpana Gupta
 

Similar to C (20)

C introduction by piyushkumar
C introduction by piyushkumarC introduction by piyushkumar
C introduction by piyushkumar
 
Brief introduction to the c programming language
Brief introduction to the c programming languageBrief introduction to the c programming language
Brief introduction to the c programming language
 
C.ppt
C.pptC.ppt
C.ppt
 
Functions And Header Files In C++ | Bjarne stroustrup
Functions And Header Files In C++ | Bjarne stroustrupFunctions And Header Files In C++ | Bjarne stroustrup
Functions And Header Files In C++ | Bjarne stroustrup
 
C Programming Training in Ambala ! Batra Computer Centre
C Programming Training in Ambala ! Batra Computer CentreC Programming Training in Ambala ! Batra Computer Centre
C Programming Training in Ambala ! Batra Computer Centre
 
88 c-programs
88 c-programs88 c-programs
88 c-programs
 
88 c programs 15184
88 c programs 1518488 c programs 15184
88 c programs 15184
 
Getting Started Cpp
Getting Started CppGetting Started Cpp
Getting Started Cpp
 
C programming session 01
C programming session 01C programming session 01
C programming session 01
 
Report on c and c++
Report on c and c++Report on c and c++
Report on c and c++
 
C programming day#1
C programming day#1C programming day#1
C programming day#1
 
C tutorial
C tutorialC tutorial
C tutorial
 
Functions.pptx, programming language in c
Functions.pptx, programming language in cFunctions.pptx, programming language in c
Functions.pptx, programming language in c
 
Quiz 9
Quiz 9Quiz 9
Quiz 9
 
Structures-2
Structures-2Structures-2
Structures-2
 
270_1_CIntro_Up_To_Functions.ppt
270_1_CIntro_Up_To_Functions.ppt270_1_CIntro_Up_To_Functions.ppt
270_1_CIntro_Up_To_Functions.ppt
 
Survey of programming language getting started in C
Survey of programming language getting started in CSurvey of programming language getting started in C
Survey of programming language getting started in C
 
270 1 c_intro_up_to_functions
270 1 c_intro_up_to_functions270 1 c_intro_up_to_functions
270 1 c_intro_up_to_functions
 
270_1_CIntro_Up_To_Functions.ppt
270_1_CIntro_Up_To_Functions.ppt270_1_CIntro_Up_To_Functions.ppt
270_1_CIntro_Up_To_Functions.ppt
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 

More from Anuja Lad

Questions from chapter 1 data communication and networking
Questions from chapter 1 data communication and networkingQuestions from chapter 1 data communication and networking
Questions from chapter 1 data communication and networking
Anuja Lad
 
T y b com question paper of mumbai university
T y b com question paper of mumbai universityT y b com question paper of mumbai university
T y b com question paper of mumbai university
Anuja Lad
 
Questions from chapter 1 data communication and networking
Questions from chapter 1 data communication and networkingQuestions from chapter 1 data communication and networking
Questions from chapter 1 data communication and networking
Anuja Lad
 
T y b com question paper of mumbai university
T y b com question paper of mumbai universityT y b com question paper of mumbai university
T y b com question paper of mumbai university
Anuja Lad
 
Data communication
Data communicationData communication
Data communication
Anuja Lad
 
Data communication intro
Data communication introData communication intro
Data communication intro
Anuja Lad
 
Intro net 91407
Intro net 91407Intro net 91407
Intro net 91407
Anuja Lad
 
Mysqlppt3510
Mysqlppt3510Mysqlppt3510
Mysqlppt3510
Anuja Lad
 
Introductionto excel2007
Introductionto excel2007Introductionto excel2007
Introductionto excel2007
Anuja Lad
 
1 introduction-to-computer-networking
1 introduction-to-computer-networking1 introduction-to-computer-networking
1 introduction-to-computer-networking
Anuja Lad
 

More from Anuja Lad (14)

Important topic in board exams
Important topic in board examsImportant topic in board exams
Important topic in board exams
 
Basic networking hardware pre final 1
Basic networking hardware pre final 1Basic networking hardware pre final 1
Basic networking hardware pre final 1
 
Data communication
Data communicationData communication
Data communication
 
Data communication intro
Data communication introData communication intro
Data communication intro
 
Questions from chapter 1 data communication and networking
Questions from chapter 1 data communication and networkingQuestions from chapter 1 data communication and networking
Questions from chapter 1 data communication and networking
 
T y b com question paper of mumbai university
T y b com question paper of mumbai universityT y b com question paper of mumbai university
T y b com question paper of mumbai university
 
Questions from chapter 1 data communication and networking
Questions from chapter 1 data communication and networkingQuestions from chapter 1 data communication and networking
Questions from chapter 1 data communication and networking
 
T y b com question paper of mumbai university
T y b com question paper of mumbai universityT y b com question paper of mumbai university
T y b com question paper of mumbai university
 
Data communication
Data communicationData communication
Data communication
 
Data communication intro
Data communication introData communication intro
Data communication intro
 
Intro net 91407
Intro net 91407Intro net 91407
Intro net 91407
 
Mysqlppt3510
Mysqlppt3510Mysqlppt3510
Mysqlppt3510
 
Introductionto excel2007
Introductionto excel2007Introductionto excel2007
Introductionto excel2007
 
1 introduction-to-computer-networking
1 introduction-to-computer-networking1 introduction-to-computer-networking
1 introduction-to-computer-networking
 

Recently uploaded

Recently uploaded (20)

Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
 
Introduction to Open Source RAG and RAG Evaluation
Introduction to Open Source RAG and RAG EvaluationIntroduction to Open Source RAG and RAG Evaluation
Introduction to Open Source RAG and RAG Evaluation
 
UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2
 
In-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT ProfessionalsIn-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT Professionals
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
IESVE for Early Stage Design and Planning
IESVE for Early Stage Design and PlanningIESVE for Early Stage Design and Planning
IESVE for Early Stage Design and Planning
 
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
 
IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
 
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya HalderCustom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
 
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone KomSalesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
Powerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara LaskowskaPowerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara Laskowska
 
UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1
 

C

  • 1. Brief Introduction to the C Programming Language Fred Kuhns [email_address] Applied Research Laboratory, Department of Computer Science and Engineering, Washington University in St. Louis
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7. Another Example C Program example.c /* this is a C-style comment * You generally want to palce * all file includes at start of file * */ #include <stdio.h> #include <stdlib.h> int main (int argc, char **argv) { // this is a C++-style comment // printf prototype in stdio.h printf(“Hello, Prog name = %s”, argv[0]); exit(0); } /* comments */ #ifndef _STDIO_H #define _STDIO_H ... definitions and protoypes #endif /usr/include/stdio.h /* prevents including file * contents multiple * times */ #ifndef _STDLIB_H #define _STDLIB_H ... definitions and protoypes #endif /usr/include/stdlib.h #include directs the preprocessor to “include” the contents of the file at this point in the source file. #define directs preprocessor to define macros.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16. Pointers in C (and C++) Address 0x3dc 0x3d8 Program Memory 0x3cc 0x3c8 0x3c4 0x3c0 Note: The compiler converts z[1] or *(z+1) to Value at address ( Address of z + sizeof(int)) ; In C you would write the byte address as: (char *)z + sizeof(int); or letting the compiler do the work for you (int *)z + 1; Step 1 : int main (int argc, argv) { int x = 4; int * y = & x ; int * z [4] = { NULL , NULL , NULL , NULL }; int a [4] = {1, 2, 3, 4}; ... 0x3bc 0x3b8 0x3b4 0x3b0 0x3d4 0x3d0 z[3] z[2] z[1] z[0] a[3] a[2] a[1] a[0] x y 4 0x3dc 0 0 0 0 4 3 2 1 NA NA
  • 17. Pointers Continued 4 0x3dc Address 0x3dc 0x3d8 Program Memory 0x3bc 0x3b8 0x3b4 0x3b0 0x3cc 0x3c8 0x3c4 0x3c0 Step 1: int main (int argc, argv) { int x = 4; int *y = &x; int *z[4] = {NULL, NULL, NULL, NULL}; int a[4] = {1, 2, 3, 4}; Step 2 : Assign addresses to array Z z [0] = a; // same as & a [0] ; z [1] = a + 1; // same as &a[1] ; z [2] = a + 2; // same as &a[2]; z [3] = a + 3; // same as &a[3]; 0x3bc 0x3b8 0x3b4 0x3b0 4 3 2 1 NA 0x3d4 0x3d0 z[3] z[2] z[1] z[0] a[3] a[2] a[1] a[0] NA x y
  • 18. Pointers Continued 4 0x3dc Address 0x3dc 0x3d8 Program Memory 0x3bc 0x3b8 0x3b4 0x3b0 0x3cc 0x3c8 0x3c4 0x3c0 Step 1: int main (int argc, argv) { int x = 4; int *y = &x; int *z[4] = {NULL, NULL, NULL, NULL}; int a[4] = {1, 2, 3, 4}; Step 2: z [0] = a; z [1] = a + 1; z [2] = a + 2; z [3] = a + 3; Step 3 : No change in z’s values z[0] = (int *)((char *)a); z[1] = (int *)((char *)a + sizeof(int)); z[2] = (int *)((char *)a + 2 * sizeof(int)); z[3] = (int *)((char *)a + 3 * sizeof(int)); 0x3bc 0x3b8 0x3b4 0x3b0 4 3 2 1 NA 0x3d4 0x3d0 z[3] z[2] z[1] z[0] a[3] a[2] a[1] a[0] NA x y
  • 19.
  • 20. After Preprocessing and Compiling typedef struct wth_t { int state; QNODE(wth_t) alist; } wth_t; # define QNODE (type) struct { struct type *next; struct type **prev; } typedef struct wth_t { int state; struct { struct wth_t *next; struct wth_t **prev; } alist; } wth_t; < integer > state < address > next < address > prev 3 words in memory # define QNODE_INIT (node, field) do { (node)->field.next = (node); (node)->field.prev = &(node)->field.next;} while ( /* */ 0 ); 0 0x00100 0x00104 0x100 head : instance of wth_t 0x104 0x108 memory layout after GCC CPP QNODE_INIT(head, alist)
  • 21. QNODE Manipulations # define QINSERT_BEFORE ( head , node , alist )do { *( head )-> alist .prev = ( node ); ( node )-> alist .prev = ( head )-> alist .prev; ( head )-> alist .prev = &( node )-> alist. next;( node )-> alist .next = ( head ); } while (/* */0) QINSERT_BEFORE(head, node0, alist); ? before 0x100 0 0x100 0x104 head 0x104 0x108 0x1a0 0 0x1a0 0x1a4 node0 0x1a4 0x1a8
  • 22. QNODE Manipulations # define QINSERT_BEFORE ( head , node , alist )do { *( head )-> alist .prev = ( node ); (node)->alist.prev = (head)->alist.prev; (head)->alist.prev = &(node)->alist.next;(node)->alist.next = (head); } while (/* */0) QINSERT_BEFORE(head, node0, alist); before 0x100 0 0x100 0x104 head 0x104 0x108 0x1a0 0 0x1a0 0x1a4 node0 0x1a4 0x1a8 0x100 0 0x1a0 0x104 head 0x104 0x108 0x1a0 0 0x1a0 0x1a4 node0 0x1a4 0x1a8
  • 23. QNODE Manipulations # define QINSERT_BEFORE ( head , node , alist )do { *( head )-> alist .prev = ( node ); ( node )-> alist .prev = ( head )-> alist .prev; ( head )-> alist .prev = &( node )-> alist. next;( node )-> alist .next = ( head ); } while (/* */0) QINSERT_BEFORE(head, node0, alist); before 0x100 0 0x100 0x104 head 0x104 0x108 0x1a0 0 0x1a0 0x1a4 node0 0x1a4 0x1a8 0x100 0 0x1a0 0x104 head 0x104 0x108 0x1a0 0 0x1a0 0x104 node0 0x1a4 0x1a8
  • 24. QNODE Manipulations # define QINSERT_BEFORE ( head , node , alist )do { *( head )-> alist .prev = ( node ); ( node )-> alist .prev = ( head )-> alist .prev; ( head )-> alist .prev = &( node )-> alist. next;( node )-> alist .next = ( head ); } while (/* */0) QINSERT_BEFORE(head, node0, alist); before 0x100 0 0x100 0x104 head 0x104 0x108 0x1a0 0 0x1a0 0x1a4 node0 0x1a4 0x1a8 0x100 0 0x1a0 0x1a4 head 0x104 0x108 0x1a0 0 0x1a0 0x104 node0 0x1a4 0x1a8
  • 25. QNODE Manipulations # define QINSERT_BEFORE ( head , node , alist )do { *( head )-> alist .prev = ( node ); ( node )-> alist .prev = ( head )-> alist .prev; ( head )-> alist .prev = &( node )-> alist. next;( node )-> alist .next = ( head ); } while (/* */0) QINSERT_BEFORE(head, node0, alist); before 0x100 0 0x100 0x104 head 0x104 0x108 0x1a0 0 0x1a0 0x1a4 node0 0x1a4 0x1a8 0x100 0 0x1a0 0x1a4 head 0x104 0x108 0x1a0 0 0x100 0x104 node0 0x1a4 0x1a8
  • 26. QNODE Manipulations # define QINSERT_BEFORE ( head , node , alist )do { *( head )-> alist .prev = ( node ); ( node )-> alist .prev = ( head )-> alist .prev; ( head )-> alist .prev = &( node )-> alist. next;( node )-> alist .next = ( head ); } while (/* */0) QINSERT_BEFORE(head, node0, alist); before 0x100 0 0x100 0x104 head 0x104 0x108 0x1a0 0 0x1a0 0x1a4 node0 0x1a4 0x1a8 0x100 0 0x1a0 0x1a4 head 0x104 0x108 0x1a0 0 0x100 0x104 node0 0x1a4 0x1a8
  • 27. Adding a Third Node QINSERT_BEFORE(head, node1, alist); # define QINSERT_BEFORE ( head , node , alist )do { *( head )-> alist .prev = ( node ); ( node )-> alist .prev = ( head )-> alist .prev; ( head )-> alist .prev = &( node )-> alist. next; ( node )-> alist .next = ( head ); } while (/* */0) 0x100 0 0x1a0 0x1a4 head 0x104 0x108 0x1a0 0 0x100 0x104 node0 0x1a4 0x1a8 0x200 0 0x200 0x204 node1 0x204 0x208 0x200 0 0x200 0x204 node1 0x204 0x208 0x100 0 0x1a0 0x1a4 head 0x104 0x108 0x1a0 0 0x100 0x104 node0 0x1a4 0x1a8
  • 28. Adding a Third Node QINSERT_BEFORE(head, node1, alist); # define QINSERT_BEFORE ( head , node1 , alist )do { *( head )-> alist .prev = ( node1 ); ( node1 )-> alist .prev = ( head )-> alist .prev; ( head )-> alist .prev = &( node1 )-> alist. next; ( node1 )-> alist .next = ( head ); } while (/* */0) (1) (1) 0x100 0 0x1a0 0x1a4 head 0x104 0x108 0x1a0 0 0x100 0x104 node0 0x1a4 0x1a8 0x100 0 0x1a0 0x1a4 head 0x104 0x108 0x1a0 0 0x200 0x104 node0 0x1a4 0x1a8 0x200 0 0x200 0x204 node1 0x204 0x208 0x200 0 0x200 0x204 node1 0x204 0x208
  • 29. Adding a Third Node QINSERT_BEFORE(head, node1, alist); # define QINSERT_BEFORE ( head , node1 , alist )do { *( head )-> alist .prev = ( node1 ); ( node1 )-> alist .prev = ( head )-> alist .prev; ( head )-> alist .prev = &( node1 )-> alist. next; ( node1 )-> alist .next = ( head ); } while (/* */0) (1) (2) (2) 0x100 0 0x1a0 0x1a4 head 0x104 0x108 0x1a0 0 0x100 0x104 node0 0x1a4 0x1a8 0x100 0 0x1a0 0x1a4 head 0x104 0x108 0x1a0 0 0x200 0x104 node0 0x1a4 0x1a8 0x200 0 0x200 0x204 node1 0x204 0x208 0x200 0 0x200 0x1a4 node1 0x204 0x208
  • 30. Adding a Third Node QINSERT_BEFORE(head, node1, alist); # define QINSERT_BEFORE ( head , node1 , alist )do { *( head )-> alist .prev = ( node1 ); ( node1 )-> alist .prev = ( head )-> alist .prev; ( head )-> alist .prev = &( node1 )-> alist. next; ( node1 )-> alist .next = ( head ); } while (/* */0) (3) (3) 0x100 0 0x1a0 0x1a4 head 0x104 0x108 0x1a0 0 0x100 0x104 node0 0x1a4 0x1a8 0x100 0 0x1a0 0x204 head 0x104 0x108 0x1a0 0 0x200 0x104 node0 0x1a4 0x1a8 0x200 0 0x200 0x204 node1 0x204 0x208 0x200 0 0x200 0x1a4 node1 0x204 0x208 (1) (1) (2) (2)
  • 31. Adding a Third Node QINSERT_BEFORE(head, node1, alist); # define QINSERT_BEFORE ( head , node1 , alist )do { *( head )-> alist .prev = ( node1 ); ( node1 )-> alist .prev = ( head )-> alist .prev; ( head )-> alist .prev = &( node1 )-> alist. next; ( node1 )-> alist .next = ( head ); } while (/* */0) (1) (1) (2) (2) (3) (3) (4) (4) 0x100 0 0x1a0 0x1a4 head 0x104 0x108 0x1a0 0 0x100 0x104 node0 0x1a4 0x1a8 0x100 0 0x1a0 0x204 head 0x104 0x108 0x1a0 0 0x200 0x104 node0 0x1a4 0x1a8 0x200 0 0x200 0x204 node1 0x204 0x208 0x200 0 0x100 0x1a4 node1 0x204 0x208
  • 32. Removing a Node QREMOVE(node0, alist); # define QREMOVE ( node , alist ) do { (1) *(( node )-> alist .prev) = ( node )-> alist .next; (2) (( node )-> alist .next)-> alist .prev = ( node )-> alist .prev;(3) ( node )-> alist .next = ( node ); (4) ( node )-> alist .prev = &(( node )-> alist .next); } while ( /* */ 0) 0x100 0 0x1a0 0x204 head 0x104 0x108 0x1a0 0 0x200 0x104 node0 0x1a4 0x1a8 0x200 0 0x100 0x1a4 node1 0x204 0x208 0x100 0 ?? ?? head 0x104 0x108 0x1a0 0 ?? ?? node0 0x1a4 0x1a8 0x200 0 ?? ?? node1 0x204 0x208
  • 33. Removing a Node QREMOVE(node0, alist); # define QREMOVE ( node , alist ) do { *(( node )-> alist .prev) = ( node )-> alist .next; (( node )-> alist .next)-> alist .prev = ( node )-> alist .prev; ( node )-> alist .next = ( node ); ( node )-> alist .prev = &(( node )-> alist .next); } while ( /* */ 0) 0x100 0 0x1a0 0x204 head 0x104 0x108 0x1a0 0 0x200 0x104 node0 0x1a4 0x1a8 0x200 0 0x100 0x1a4 node1 0x204 0x208 0x100 0 0x1a0 0x204 head 0x104 0x108 0x1a0 0 0x200 0x104 node0 0x1a4 0x1a8 0x200 0 0x100 0x1a4 node1 0x204 0x208
  • 34. Removing a Node QREMOVE(node0, alist); # define QREMOVE ( node0 , alist ) do { (1) *(( node0 )->alist.prev) = ( node0 )->alist.next; (( node0 )->alist.next)->alist.prev = ( node0 )->alist.prev; ( node0 )->alist.next = ( node0 ); ( node0 )->alist.prev = &(( node0 )->alist.next); } while ( /* */ 0) (1) 0x100 0 0x200 0x204 head 0x104 0x108 0x1a0 0 0x200 0x104 node0 0x1a4 0x1a8 0x200 0 0x100 0x1a4 node1 0x204 0x208 0x100 0 0x1a0 0x204 head 0x104 0x108 0x1a0 0 0x200 0x104 node0 0x1a4 0x1a8 0x200 0 0x100 0x1a4 node1 0x204 0x208
  • 35. Removing a Node QREMOVE(node0, alist); # define QREMOVE ( node0 , alist ) do { *(( node0 )->alist.prev) = ( node0 )->alist.next; (2) (( node0 )->alist.next)->alist.prev = ( node0 )->alist.prev;( node0 )->alist.next = ( node0 ); ( node0 )->alist.prev = &(( node0 )->alist.next); } while ( /* */ 0) (2) 0x100 0 0x200 0x204 head 0x104 0x108 0x1a0 0 0x200 0x104 node0 0x1a4 0x1a8 0x200 0 0x100 0x104 node1 0x204 0x208 0x100 0 0x1a0 0x204 head 0x104 0x108 0x1a0 0 0x200 0x104 node0 0x1a4 0x1a8 0x200 0 0x100 0x1a4 node1 0x204 0x208
  • 36. Removing a Node QREMOVE(node0, alist); # define QREMOVE ( node0 , alist ) do { *(( node0 )->alist.prev) = ( node0 )->alist.next; (( node0 )->alist.next)->alist.prev = ( node0 )->alist.prev;(3) ( node0 )->alist.next = ( node0 ); ( node0 )->alist.prev = &(( node0 )->alist.next); } while ( /* */ 0) (3) 0x100 0 0x200 0x204 head 0x104 0x108 0x1a0 0 0x1a0 0x104 node0 0x1a4 0x1a8 0x200 0 0x100 0x104 node1 0x204 0x208 0x100 0 0x1a0 0x204 head 0x104 0x108 0x1a0 0 0x200 0x104 node0 0x1a4 0x1a8 0x200 0 0x100 0x1a4 node1 0x204 0x208
  • 37. Removing a Node QREMOVE(node0, alist); # define QREMOVE ( node0 , alist ) do { *(( node0 )->alist.prev) = ( node0 )->alist.next; (( node0 )->alist.next)->alist.prev = ( node0 )->alist.prev;( node0 )->alist.next = ( node0 ); (4) ( node0 )->alist.prev = &(( node0 )->alist.next); } while ( /* */ 0) (4) 0x100 0 0x200 0x204 head 0x104 0x108 0x1a0 0 0x1a0 0x1a4 node0 0x1a4 0x1a8 0x200 0 0x100 0x104 node1 0x204 0x208 0x100 0 0x1a0 0x204 head 0x104 0x108 0x1a0 0 0x200 0x104 node0 0x1a4 0x1a8 0x200 0 0x100 0x1a4 node1 0x204 0x208
  • 38. Solution to Removing a Node QREMOVE(node0, alist); # define QREMOVE ( node , alist ) do { (1) *(( node )-> alist .prev) = ( node )-> alist .next; (2) (( node )-> alist .next)-> alist .prev = ( node )-> alist .prev;(3) ( node )-> alist .next = ( node ); (4) ( node )-> alist .prev = &(( node )-> alist .next); } while ( /* */ 0) 0x100 0 0x1a0 0x204 head 0x104 0x108 0x1a0 0 0x200 0x104 node0 0x1a4 0x1a8 0x200 0 0x100 0x1a4 node1 0x204 0x208 0x100 0 0x200 0x204 head 0x104 0x108 0x1a0 0 0x1a0 0x1a4 node0 0x1a4 0x1a8 0x200 0 0x100 0x104 node1 0x204 0x208
  • 39.
  • 40.
  • 41. Operator Precedence (from “C a Reference Manual”, 5 th Edition) 15 16 Precedence right-to-left unary address of & right-to-left unary indirection ( dereference ) * right-to-left unary negation, plus - + right-to-left unary logical not ! right-to-left unary bitwise not ~ right-to-left prefix increment, decrement ++ -- left-to-right postfix compound literal ( type ){ init } left-to-right postfix increment, decrement ++ -- left-to-right postfix direct selection . left-to-right postfix function call f (...) left-to-right postfix subscripting a [k] size indirect selection simple tokens Operator n/a primary names, literals right-to-left unary sizeof left to right postfix -> Associates Class Tokens 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Precedence right-to-left unary casts ( type ) left-to-right binary multiplicative * / % left-to-right binary additive + - sequential eval. assignment conditional logical or logical and bitwise or bitwise xor bitwise and equality/ineq. relational left, right shift Operator left-to-right binary , right-to-left binary = += -= *= /= %= &= ^= |= <<= >>= right-to-left ternary ?: left-to-right binary || left-to-right binary && left-to-right binary | left-to-right binary ^ left-to-right binary & left-to-right binary == != left-to-right binary < <= > >= left-to-right binary << >> Associates Class Tokens
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.