1

New to C++ My understanding is endl will add a new line. So with the following piece of code:

#include <iostream>

using namespace std;

void printf(string message);

int main()
{

cout << "Hello" << endl;
cout << "World" << endl;

printf("Hello");
printf("World");

return 0;

}

void printf(string message) {
cout << message << endl;
}

I expect the output to be:

Hello

World

Hello

World

But, strangely, the output is:

Hello

World

HelloWorld

Looks like, when called from the user-defined method, endl is not adding new line..?? What is wrong with my understanding here. Please advise.

  • 5
    Are you sure it is *your* printf() that is called? I suspect it is the CRT version... – U. W. Feb 03 '22 at 12:03
  • Maybe one should be a tad bit more careful with namespaces then?! – Standard_101 Feb 03 '22 at 12:07
  • Due to overload resolution the built in `printf` function is selected over your custom defined `printf` function. To solve this, replace the `printf` calls with : `printf(string("Hello"));` – Jason Feb 03 '22 at 12:07

4 Answers4

3

The problem is that due to overload resolution the built in printf function is selected over your custom defined printf function. This is because the string literal "Hello" and "World" decays to const char* due to type decay and the built in printf function is a better match than your custom defined printf.

To solve this, replace the printf calls with :

printf(std::string("Hello"));
printf(std::string("World"));

In the above statements, we're explicitly using std::string's constructor to create std::string objects from the string literal "Hello" and "World" and then passing those std::string objects by value to your printf function.

Another alternative is to put your custom printf inside a custom namespace. Or you can name your function other than printf itself.

Jason
  • 36,170
  • 5
  • 26
  • 60
2

It's using the inbuilt printf method. Try to explicitly use std::string so that it'll call custom printf method.

printf(std::string("Hello"));
printf(std::string("World"));

Or you can put your method in a different namespace:

#include <iostream>

namespace test
{
    extern void printf(const std::string& message);
}

int main()
{
    std::cout << "Hello" << std::endl;
    std::cout << "World" << std::endl;

    test::printf("Hello");
    test::printf("World");

    return 0;

}

void test::printf(const std::string& message) {
    std::cout << message << std::endl;
}
Ronen
  • 195
  • 7
0

You should pick function name other than printf(); like Print().

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
Harsh Panchal
  • 146
  • 1
  • 1
  • 6
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 04 '22 at 06:31
0

try renaming the "printf" function to "print" it works fine-

#include <iostream>
using namespace std;
void print(string message);

int main()
{

cout << "Hello" << endl;
cout << "World" << endl;

print("Hello");
print("World");
cout <<endl;
return 0;

}

void print(std::string message) {
cout << message << endl;
}
Suman
  • 354
  • 3
  • 10