SlideShare a Scribd company logo
1 of 51
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

C++ Advanced
C++ AdvancedC++ Advanced
C++ AdvancedVivek Das
 
Objective c beginner's guide
Objective c beginner's guideObjective c beginner's guide
Objective c beginner's guideTiago Faller
 
2018 cosup-delete unused python code safely - english
2018 cosup-delete unused python code safely - english2018 cosup-delete unused python code safely - english
2018 cosup-delete unused python code safely - englishJen Yee Hong
 
Extending Python - EuroPython 2014
Extending Python - EuroPython 2014Extending Python - EuroPython 2014
Extending Python - EuroPython 2014fcofdezc
 
OpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ ProgrammingOpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ ProgrammingOpen Gurukul
 
Hooking signals and dumping the callstack
Hooking signals and dumping the callstackHooking signals and dumping the callstack
Hooking signals and dumping the callstackThierry Gayet
 
CS4200 2019 | Lecture 4 | Syntactic Services
CS4200 2019 | Lecture 4 | Syntactic ServicesCS4200 2019 | Lecture 4 | Syntactic Services
CS4200 2019 | Lecture 4 | Syntactic ServicesEelco Visser
 
Declarative Type System Specification with Statix
Declarative Type System Specification with StatixDeclarative Type System Specification with Statix
Declarative Type System Specification with StatixEelco Visser
 
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]Chris Adamson
 
Paradigmas de Linguagens de Programacao - Aula #5
Paradigmas de Linguagens de Programacao - Aula #5Paradigmas de Linguagens de Programacao - Aula #5
Paradigmas de Linguagens de Programacao - Aula #5Ismar Silveira
 
C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)Saifur Rahman
 

What's hot (18)

C++ Advanced
C++ AdvancedC++ Advanced
C++ Advanced
 
Objective c beginner's guide
Objective c beginner's guideObjective c beginner's guide
Objective c beginner's guide
 
2018 cosup-delete unused python code safely - english
2018 cosup-delete unused python code safely - english2018 cosup-delete unused python code safely - english
2018 cosup-delete unused python code safely - english
 
Extending Python - EuroPython 2014
Extending Python - EuroPython 2014Extending Python - EuroPython 2014
Extending Python - EuroPython 2014
 
OpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ ProgrammingOpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ Programming
 
Hooking signals and dumping the callstack
Hooking signals and dumping the callstackHooking signals and dumping the callstack
Hooking signals and dumping the callstack
 
CS4200 2019 | Lecture 4 | Syntactic Services
CS4200 2019 | Lecture 4 | Syntactic ServicesCS4200 2019 | Lecture 4 | Syntactic Services
CS4200 2019 | Lecture 4 | Syntactic Services
 
Python Programming
Python ProgrammingPython Programming
Python Programming
 
C tutorial
C tutorialC tutorial
C tutorial
 
C tutorial
C tutorialC tutorial
C tutorial
 
Declarative Type System Specification with Statix
Declarative Type System Specification with StatixDeclarative Type System Specification with Statix
Declarative Type System Specification with Statix
 
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]
 
C Tutorials
C TutorialsC Tutorials
C Tutorials
 
Paradigmas de Linguagens de Programacao - Aula #5
Paradigmas de Linguagens de Programacao - Aula #5Paradigmas de Linguagens de Programacao - Aula #5
Paradigmas de Linguagens de Programacao - Aula #5
 
C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)
 
C++ theory
C++ theoryC++ theory
C++ theory
 
C++ via C#
C++ via C#C++ via C#
C++ via C#
 
Oop Presentation
Oop PresentationOop Presentation
Oop Presentation
 

Viewers also liked

640 802-study-guide-sample
640 802-study-guide-sample640 802-study-guide-sample
640 802-study-guide-samplerickybcool
 
5 intro to networking
5 intro to networking5 intro to networking
5 intro to networkingAnuja Lad
 
Ks 5th networking_basicskevinshea
Ks 5th networking_basicskevinsheaKs 5th networking_basicskevinshea
Ks 5th networking_basicskevinsheaPrema Bahadur
 
Introductionto excel2007
Introductionto excel2007Introductionto excel2007
Introductionto excel2007Khan Rahimeen
 

Viewers also liked (7)

Mysql2
Mysql2Mysql2
Mysql2
 
C chap02
C chap02C chap02
C chap02
 
640 802-study-guide-sample
640 802-study-guide-sample640 802-study-guide-sample
640 802-study-guide-sample
 
5 intro to networking
5 intro to networking5 intro to networking
5 intro to networking
 
Ks 5th networking_basicskevinshea
Ks 5th networking_basicskevinsheaKs 5th networking_basicskevinshea
Ks 5th networking_basicskevinshea
 
Mysqlppt3510
Mysqlppt3510Mysqlppt3510
Mysqlppt3510
 
Introductionto excel2007
Introductionto excel2007Introductionto excel2007
Introductionto excel2007
 

Similar to Brief Introduction to the C Programming Language

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 languageKumar 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 stroustrupSyedHaroonShah4
 
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 Centrejatin batra
 
88 c programs 15184
88 c programs 1518488 c programs 15184
88 c programs 15184Sumit Saini
 
Getting Started Cpp
Getting Started CppGetting Started Cpp
Getting Started CppLong Cao
 
C programming session 01
C programming session 01C programming session 01
C programming session 01Dushmanta Nath
 
Report on c and c++
Report on c and c++Report on c and c++
Report on c and c++oggyrao
 
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.pptUdhayaKumar175069
 
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 Cummeafruz
 
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_functionsray143eddie
 
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.pptAlefya1
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programmingAlpana Gupta
 

Similar to Brief Introduction to the C Programming Language (20)

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 15184
88 c programs 1518488 c programs 15184
88 c programs 15184
 
88 c-programs
88 c-programs88 c-programs
88 c-programs
 
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
 
Quiz 9
Quiz 9Quiz 9
Quiz 9
 
C tutorial
C tutorialC tutorial
C tutorial
 
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
 
88 c-programs
88 c-programs88 c-programs
88 c-programs
 

Recently uploaded

What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 

Recently uploaded (20)

What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 

Brief Introduction to the C Programming Language

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