user3809875
user3809875

Reputation: 131

Simple C++ type conversion program

I am learning C++ and am on the subject of safe and unsafe conversions. Here is my code:

#include <iostream>

using namespace std;
int main()
{
    double d = 0;
    while( cin >> d )
    {
           int i = d; // try to squeez in a double into an int
           char c = i; // try to squeez in an int into a char
           int i2 = c; // try to get the integer value of the char

           cout << "d == " << d << "\n"
                << "i == " << i << "\n"
                << "c == " << c << "\n"
                << "i2 == " << i2 << "\n";
    }
    return 0;
}

when I input 3 i get the following:

d == 3
i == 3
c == a heart shape
i2 == 3

why does c print out a hear shape like this: link

Upvotes: 3

Views: 167

Answers (5)

leemes
leemes

Reputation: 45705

Although char is an integer type,ostream (the type of cout) treats it special. It prints it as a single character instead of a number.

If you don't want this, just "force" it to be printed as a number by writing int(c) instead of c.

As you began your question with a concern about safe and unsafe conversions, I'll add a comment: int(c) is not the same as (int)c, which you'll also see very often (sadly). In this case (with int) it has the same effect, but it has a different semantic. The first constructs an int value from an existing char value. The second is an unsafe conversion using the C-style cast which should be avoided in C++ since it can kill all type-safety you gain with C++ in the first place.

Upvotes: 1

Reid Spencer
Reid Spencer

Reputation: 2785

The value 3 assigned from i to c is truncated to a character's width. It then represents the ascii character Ctrl-C, or ETX, (0x03) which is not normally a printable character. However, many terminal types will attempt to print something anyway. Depending on your terminal, it may print as a heart. Without knowing the character encoding of your terminal and its settings, it is difficult to say exactly why it printed as a heart shape.

Upvotes: 1

GreenAsJade
GreenAsJade

Reputation: 14685

You are seeing a rendering of the unprintable character represented by a char of value '3'.

In your code, the variable c has the value 3. This is actually a "non printing control character" in the ASCII set. (The character '3' is ASCII 33).

It has a representation of the heart symbol when it is attempted to be printed.

Upvotes: 0

Joao
Joao

Reputation: 619

for the 'd', you're outputting a double, which is converted to the string of characters that represent the contents of the double in binary format. The same happens for the integers (your i and i2).

The char is sent to the screen without being converted, i.e., is sent as it. Char 3, if you look up in an ASCII table, doesn't really represent anything - printable chars begin at 32. So, you get a "weird" character.

Upvotes: 0

Topological Sort
Topological Sort

Reputation: 2851

Because the heart character is the symbol with value 3 in the PC version of the ASCII table. Here is a listing: http://www.ascii-codes.com/ . You may find other nice to have codes there, assuming your program is running in that environment.

Upvotes: 1

Related Questions