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

What is the output of the following program?

#include <stdio.h>
int main()
{
    int var;
    var = 1; 
    var = 2;
/*  var = 3; */
    print("%d", var);
    return 0;
}
  • 2
  • 3
  • 6
  • 1
Explanation & Hints:

The output of the provided C program will be 2. Here’s a step-by-step explanation:

  1. int var; declares an integer variable named var.
  2. var = 1; sets the value of var to 1.
  3. var = 2; then changes the value of var to 2.
  4. /* var = 3; */ is commented out and thus has no effect on the program’s execution.
  5. print("%d", var); should be printf("%d", var); to correctly compile in C. Assuming this is a typo and should be printf, it will print the current value of var, which is 2.

Therefore, the correct output, assuming the typo is corrected to printf, is 2.

For more Questions and Answers:

Introduction to computer programming, variables, and comments Module 1 Test Answers Full 100%

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