Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9240243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:09:21+00:00 2026-06-18T08:09:21+00:00

Possible Duplicate: Why does std::cout output disappear completely after NULL is sent to it

  • 0

Possible Duplicate:
Why does std::cout output disappear completely after NULL is sent to it

It seems if you try:

 std::cout << NULL << endl;
 std::cout << "hell" << endl;

it print out nothing and C++ IO stops working for all subsequent outputs.

but it works fine in C stdio:

 printf("%s\n", NULL);
 printf("%s\n", "hell"); 

(null)

hell

Is there any good reason why C++ IO can’t do the same thing?

(edited in response to comments)
alright, to make it clear, NULL does have a type, say const char*

const char* getxxx();  // may return NULL, 
cout << getxxx();      // won't work if NULL returned
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:09:22+00:00Added an answer on June 18, 2026 at 8:09 am

    Huh? I see no reason why cout should fail simply because you executed

    std::cout << 0 << std::endl;
    

    It should output 0\n. And it does. End of story.

    (In case you’re confused, please know that in C++, #define NULL (0).)

    In case you wrote:

    T* p = 0;
    std::cout << p << std::endl;
    

    then it will display the address 0, (generally in hexadecimal and padded to the pointer size, since this is the preferred way of looking at pointers).

    (This is btw the behavior you would get using the C definition of NULL, which is #define NULL ((void*)0).)

    Only if you write

    char* p = 0;
    std::cout << p << std::endl;
    

    are you in trouble. Now you’re calling

    template<class traits>
    basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>& out, const char* s);
    

    for which the Standard (section 27.7.3.6.4) says:

    Requires: s shall not be a null pointer.

    When you do pass a null pointer, the rule 17.6.4.9 applies, which states that:

    Each of the following applies to all arguments to functions defined in the C++ standard library, unless explicitly stated otherwise.
    * If an argument to a function has an invalid value (such as a value outside the domain of the function or a pointer invalid for its intended use), the behavior is undefined.

    So you’re in the land of “undefined behavior”. There’s no guarantee that failbit gets set and the program continues.


    Please note that printf behavior didn’t actually depend on the type of NULL. It’s the format string "%s" that caused treatment as a string (pointer to NUL-terminated character sequence).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: what does std::endl represent exactly on each platform? I'm trying to figure
Possible Duplicate: C++ Vector Pointers to Objects Does std::list::remove method call destructor of each
Possible Duplicate: Are std::vector elements guaranteed to be contiguous? does std::vector always contain the
Possible Duplicate: iterator validity ,after erase() call in std::set When I iterate over a
Possible Duplicate: Does std::list::remove method call destructor of each removed element? Assume I have
Possible Duplicate: Does std::list::remove method call destructor of each removed element? I have a
Possible Duplicate: C++: “std::endl” vs “\n” In Accelerated C++ , two things are mentioned:
Possible Duplicate: does code in finally get run after a return in objective-c? Consider
Possible Duplicate: std::bind a bound function void foo0(int val) { std::cout << val <<
Possible Duplicate: Why does this simple std::thread example not work? Code: #include <iostream> #include

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.