What is the output of the following C program? void m() { printf("hi"); } int main() { m(); return 0; }

Difficulty: Easy

Correct Answer: hi

Explanation:


Introduction / Context:
This question checks a very basic concept in C programming: calling a function that performs a printf. It also reinforces that functions must be called from main in order for their code to execute.


Given Data / Assumptions:

  • There is a function m that calls printf("hi").
  • The main function calls m and then returns 0.
  • We assume the appropriate standard I or O header has been included in the original code so that printf is declared.


Concept / Approach:
When a C program starts, execution begins in main. Any code in other functions executes only if they are called from main (directly or indirectly). In this program, main calls m, which causes control flow to enter m, run printf, and then return to main. The printf function writes the characters h and i to standard output. No newline character is printed, so the cursor remains on the same line.


Step-by-Step Solution:
Step 1: Program execution enters main.Step 2: main calls m().Step 3: Inside m, printf("hi") is executed, which writes the string "hi" to standard output.Step 4: Function m returns to main.Step 5: main returns 0 and the program terminates.


Verification / Alternative check:
You can test a similar program in any C compiler. It will display hi without a newline. If you add a newline character, such as printf("hi"), the output still visibly reads hi and moves to the next line.


Why Other Options Are Wrong:
Option B suggests no output, which would be true only if m were never called or if printf were never executed.Option C claims a compilation error, but the code shown is syntactically correct given the proper header.Option D suggests a runtime error, but there is nothing in this simple function that would cause a crash on a normal system.


Common Pitfalls:
Beginners sometimes forget to call a function they defined, expecting it to execute automatically. This example emphasizes that defined functions run only when explicitly invoked.


Final Answer:
The program simply prints hi.

More Questions from Programming

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion