Machine-translated by Google. Questions are authored in English.
Top

Discussion

Comment on the output of this C code?

#include <stdio.h>
    int const print()
    {
        printf("Sanfoundry.com");
        return 0;
    }
    void main()
    {
        print();
    }

 

  • A.Error because function name cannot be preceded by const
  • B.Sanfoundry.com
  • C.Sanfoundry.com is printed infinite times
  • D.Blank screen, no output

Answer: B

Correct answer: Sanfoundry.com

The string is printed once and the program ends. Applying const to a function's return type qualifies the returned value; for a return type of int it has essentially no effect, since the value is a copy that the caller here discards.

It is certainly not an error - a return type may be qualified - and nothing in the code causes repetition, print() being called once from main. The trap is reading int const print() as something unusual; it is an ordinary function with a redundant qualifier.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.