UNB/ CS/ David Bremner/ teaching/ old/ cs2023/ events/ Tutorial Exercise 2

Exercises

For each of the following program fragments, first try to work out what the output will be, and write down your guess. Next run the code and check your answers. All variables are integers.

  1. i = 6;
    j = i += i;
    printf("%d %d\n",i,j);
    
  2. i = 5;
    j = (i -= 2) +1;
    printf("%d %d\n",i,j);
    
  3. i=10; j=5;
    printf("%d ", i++ - --j);
    printf("%d %d\n",i,j);
    
  4. i=3; j=4; k=5;
    printf("%d ",i++ - j++ + --k);
    printf("%d %d %d",i,j,k);
    
  5. i=5;
    j= ++i * 3 -2;
    printf("%d %d\n",i,j);
    
  6. i=7;
    j=3 + --i * 2;
    printf("%d %d\n",i,j);