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

  • SEARCH
  • Home
  • 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 6235553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:41:13+00:00 2026-05-24T10:41:13+00:00

This code… #include <iostream> int main(int argc, char * argv[]) { char c =

  • 0

This code…

#include <iostream>

int main(int argc, char * argv[])
{
    char c = 'A';
    std::cout << &c;
    return 0;
}

…correctly outputs “A” both in Eclipse debug mode and on the command line.

However when I modify the code to…

#include <iostream>

int main(int argc, char * argv[])
{
    char c = 'A';
    bool b = true;
    std::cout << &c;
    return 0;
}

…it outputs “A␁” (the latin letter ‘A’ followed by the ‘start of header’ ASCII control character) in Eclipse debug mode and on the Windows 7 command line. Incidentally, when using bool b = false instead I don’t get the ␁ character.

I know ␀ has the value 0, and ␁ has the value 1, but why is cout << &c affected by the bool? Can anyone explain why this is?

Edit: forgot to add my environment: Windows 7 64-bit, MinGW with g++ 4.5.2, in Eclipse Indigo

  • 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-05-24T10:41:13+00:00Added an answer on May 24, 2026 at 10:41 am

    In the first version you are getting (un)lucky.

    In the second one you are getting (un)lucky (that it does not crash) but it prints more garbage.

    When you use & on a char object you get a char*. When you try and stream a char* it acts differently to all other pointers (which normally prints the address). But a char* is assumed to be a C-String. A C-String is a sequence of bytes terminated by a null character ‘\0’;

    What is actually happening is that it is printing every memory location (treating it as a char) starting at the variable ‘c’ and moving through memory until it finds the character ‘\0’

    Example 1 above:

    In your case you are lucky there happens to be a null character lying around in memory just after the variable ‘c’.

    Example 2 above:

    In your case you are lucky there happens to be a null character lying around in memory just after the variable ‘b’. But the variable ‘b’ is also be interpreted.

    • When the value of ‘b’ is false: converted to a char this is ‘\0’ (same thing). So only the letter ‘A’ is printed.
    • When the value of ‘b’ is true: converted to a char this is ‘\01’. Not ‘\0’ so it is printed. You are then lucky that the next byte is a ‘\0’ and the printing stops.

    What you actually wanted to do was:

    std::cout << c; // prints a character
    

    Or you could have created a string:

    char const* c = "A";  // Creates a null terminated C-String. Note the double quotes " "
    

    Or to get the address

    std::cout << static_cast<void*>(&c); // All other pointer types will print the address
                                         // Of the standard types only char* is treated 
                                         // differently with its own overload.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code works (C# 3) double d; if(d == (double)(int)d) ...; Is there a
This code function LoadContent(Id) { alert('Controls/Network/NetworkDetail.aspx?' + rnd() + '&CtlId=' + Id); $.get('Controls/Network/NetworkDetail.aspx?' +
This code $html->link( &raquo;, '/events/view/'.$event['Event']['id']), array('escape'=>false,'class'=>'more') ) Outputs <a href=/events/view/4> &amp;raquo;</a> instead of >>
This code compiles on msvc/g++: class A{ protected: int i; class B{ public: A*
This code does not return the correct result: Pattern p=Pattern.compile(^[y]{1,4}$|^[m]{1,4}$|^[d]{1,4}$); String text1=yyyy; String text2=mmm;
This code displays hello in chinese char buffer[10]=hello; AfxMessageBox((LPCTSTR)buffer); while this code displays it
this code produce an unexpected output: $array=str_split(abcde); foreach($array as &$item) echo $item; echo \n;
This code in JS gives me a popup saying i think null is a
This code is from Prototype.js . I've looked at probably 20 different tutorials, and
this code always returns 0 in PHP 5.2.5 for microseconds: <?php $dt = new

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.