Skip to main content

Excercise

Walkthroughs:

make sure you check out how to do walkthroughs before you start

Walkthrough 1:

What is the output of the following program?

#include <stdio.h>
int main(void){
int x=1;
int y=1;
printf("whole thing: %d\n", x = y += 3 / 4 + 5 * 2 % 3 - 6);
printf("x = %d, y = %d\n", x , y);
x = 2;
y = 2;
printf("x++ = %d\n", x++);
printf("++y = %d\n", y++);
printf("x = %d\n", x);
printf("y = %d\n", y);
return 0;
}

Walkthrough 2:

What is the output of the following program?

#include <stdio.h>
int main(void){
int x=1;
float y=1;
printf("whole thing: %d\n", x = y = (9 * 2.0 - 2 % 3 * 5) / 3);
printf("x = %d, y = %f\n", x , y);
return 0;
}

Debugging:

The following is a program that should print out the numbers 1 to 5 one per line. If it was working, this would be the output:

1
2
3
4
5

However, there are bugs in the program (both syntactic and logical).

#include <stdio.h>
int main(void)}
integer x=0;
printf("%d\n",x++);
printf("%d\n",x++);
printf("%d\n",x++);
printf("%d\n",x++);
printf("%d\n",x++);
return 0
}
  1. What are the syntax error generated when you compile the program as is?
  2. What is the cause of the syntax error(s) and how would you fix it?
  3. What logical error exist in above program?
  4. How do you fix it?