What happens if you try to compile and run this program?
#include <stdio.h>
int main(void)
{
int i, j, k;
i = 2;
j = -2;
if(i)
i--;
if(j)
j++;
k = i * j;
printf("%d",k);
return 0;
}
Explanation & Hints:
With the provided C program, here’s what will happen when it’s compiled and run:
- Variable Initialization:
i is initialized to 2.
j is initialized to -2.
- Conditional Operations:
if(i) checks if i is non-zero. Since i is 2, the condition is true, and i-- is executed, decrementing i to 1.
if(j) checks if j is non-zero. Since j is -2, the condition is also true, and j++ is executed, incrementing j to -1.
- Multiplication and Output:
k is calculated as the product of i and j , which are now 1 and -1 respectively, so k = 1 * -1 = -1 .
- The program then prints
-1 .
- Return: The program returns 0, indicating successful execution.
Thus, when this program is compiled and executed, it outputs -1 . |
For more Questions and Answers: