/* C program to accompany PMA Chapter 6 */ /* includes usually have little impact on generated code */ #include #include #include #include void structYourStuff(); int main() { int i=1,j=2; i+=j; printf("total = %d\n", i); forloop(); testcall(); structYourStuff(); } int arith() { int a=0; int b=1; a = a + 11; a = a - b; a--; b++; b = a % 3; } int ifelse() { int x=1; int y=2; if (x==y) { printf("x equals y\n"); } else { printf("x is not equal to y\n"); } } int nestedifelse(int x, int y, int z) { if (x==y) { if (z==0) { printf("z is zero and x==y\n"); } else { printf("z is not zero but still x==y\n"); } } else { if (z==0) { printf("z is zero but x!=y\n"); } else { printf("z is not zero and x!=y\n"); } } } int forloop() { int i; for (i=0; i<10; i++) { printf("i is %d\n",i); } } int testcall() { int x=0; int y=1; int z=2; int ret; ret = nestedifelse(x,y,z); } int b[5] = {123,87,487,7,978}; /* subscripts start at zero in C */ int arrayStuff() { int i; int a[5]; for (i=0; i<5; i++) { a[i] = b[i] = i; } } struct node { int x; struct node *next; }; typedef struct node pnode; void structYourStuff() { pnode *curr, *head; int i; head = NULL; for (i=1; i<=10; i++) { curr = (pnode *) malloc(sizeof(pnode)); curr->x = i; curr->next = head; head = curr; } curr = head; while(curr) { /* better to say curr != NULL */ printf("%d\n",curr->x); curr = curr->next; } }