In the following C program that uses function pointers and console functions, what output is produced on the screen? #include <stdio.h> #include <conio.h> int main() { void (*p)(); int (*q)(); int (*r)(); p = clrscr; q = getch; r = puts; (*p)(); (*r)("www.curioustab.com"); (*q)(); return 0; }

Difficulty: Medium

Correct Answer: The text www.curioustab.com is displayed and the program waits for a key press

Explanation:


Introduction / Context:
This question tests understanding of function pointers in C and basic console functions from traditional DOS style libraries. Even though clrscr and getch come from nonstandard headers, the logic of the program can still be analyzed. The goal is to determine what is printed and how the function pointers are used.


Given Data / Assumptions:

    The program declares three function pointers: p for a function returning void, q and r for functions returning int.
    p is assigned clrscr, q is assigned getch and r is assigned puts.
    The sequence of calls is to clear the screen, print a string and then wait for a key press.
    We assume a traditional environment where clrscr clears the screen, puts prints a string followed by a newline and getch waits for a key without echo.


Concept / Approach:
In C, function names decay to pointers to functions of the appropriate type, so assigning p = clrscr is valid if types are compatible. Calling a function pointer uses the same syntax as calling the function itself, either p() or (*p)(). The puts function prints the given string followed by a newline. The getch function reads a single keystroke and typically returns its code.


Step-by-Step Solution:
The program starts and assigns the addresses of clrscr, getch and puts to p, q and r respectively. The call (*p)() invokes clrscr, which clears the console screen. The call (*r)("www.curioustab.com") invokes puts with the given string, so the program prints www.curioustab.com followed by a newline. The call (*q)() invokes getch, which waits for the user to press a key before proceeding. After the key press, main returns and the program terminates.


Verification / Alternative check:
If you rewrite the code without function pointers and call clrscr(), puts("www.curioustab.com") and getch() directly, the behavior clearly matches the description. Using function pointers in this way does not change the observable output, only how the functions are referenced.


Why Other Options Are Wrong:
Option a is wrong because puts prints the string and there is nothing that suppresses this output.
Option c assumes a compilation error, but function pointer assignments are valid when the signatures match reasonably well in this environment.
Option d suggests that only a single character is printed, which would require a different function such as putchar rather than puts.


Common Pitfalls:
Learners sometimes think that using function pointers drastically changes behavior, but in most simple cases it only adds indirection. Another pitfall is to confuse nonstandard functions like clrscr and getch with standard C, but the question expects you to focus on logical behavior rather than strict portability.


Final Answer:
The program clears the screen, displays the text www.curioustab.com and then waits for the user to press a key before terminating.

More Questions from Programming

Discussion & Comments

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