LearnFrenzy
Sign in Take a free test

Functions and Default Arguments · Predict the Output · Question 15

f(0) vs f(nullptr) with int and pointer overloads Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 15 Back to Exercise

What is the output of the following C++ program?

main.cpp
#include <iostream>

void f(int)         { std::cout << "int "; }
void f(const char*) { std::cout << "pointer "; }

int main() {
    f(0);
    f(nullptr);
    f("0");
    f('0');
    std::cout << '\n';
    return 0;
}
  • A int pointer pointer int Correct
  • B pointer pointer pointer int
  • C int int pointer int
  • D int pointer pointer pointer
Correct Answer: Option A

0 is an int; nullptr converts only to pointers; "0" is a string literal (const char*); '0' is a char, promoted to int.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links