• Post author:
  • Post category:Blog
  • Reading time:2 mins read
  • Post last modified:June 12, 2024

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;
}
  • -1
  • -2
  • 1
  • 2
Explanation & Hints:

With the provided C program, here’s what will happen when it’s compiled and run:

  1. Variable Initialization:
    • i is initialized to 2.
    • j is initialized to -2.
  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.
  3. 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.
  4. Return: The program returns 0, indicating successful execution.

Thus, when this program is compiled and executed, it outputs -1.

For more Questions and Answers:

Basic data types, operations, and flow control (decision-making statements) Module 2 Test Answers Full 100%

Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments