Thread: Typecasting questions...

  1. #1
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204

    Thumbs up Typecasting questions...

    Firstly, i made the following program that's supposed to convert an int to a char and back to an int. Why does it output a negative number?

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        cout<<(int)((char)177);
        cin.get();
    }
    And secondly, why is it, on windows XP (not sure about vista) that the following program forces the computer to make a beeping sound?

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        cout<<(char)7;
        cin.get();
    }
    Thanks

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    1) For your compiler, char is apparently signed, 177 overflows, wraps around and becomes negative. Try unsigned char instead.

    2) ASCII characters with small values are "non-printing" characters, they were meant to represent actions, rather than character symbols. Char 7 happens to represent "Bell".

    Ascii table
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    Thanks... but what exactly is "Bell"?

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    ASCII code 7, known as "BEL", is used by standard (aka console mode) applications to make a sound on the inbuilt speaker of the computer. It is normally used by programs to sound an audible beep.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by yaya View Post
    Thanks... but what exactly is "Bell"?
    It's the character that on a Teletype [typewriter style "terminal"] would ring a mechanical bell [like the end-of-line bell on a classic typewriter].

    You can actually make that sound "portable" by using the char '\a' (just like newline is '\n'). [I'm not sure there are any systems that actually DON'T use 7 as a BEL character, but there may be - I expect non-ASCII-based character sets, such as EBCDIC could do that].

    --
    mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    Thanks everyone... I understand now

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    On a side note, when using C++, you are advised to use C++ type-casting. They include dynamic_cast, static_cast, reinterpret_cast and const_cast (to my knowledge; not sure if there are more).
    You could read up a little about them or someone could explain it, I guess.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Elysia View Post
    On a side note, when using C++, you are advised to use C++ type-casting. They include dynamic_cast, static_cast, reinterpret_cast and const_cast (to my knowledge; not sure if there are more).
    You could read up a little about them or someone could explain it, I guess.
    Well, yeah. But rewriting the code in the original post to use C++-style casts has no effect on the question asked.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Indeed, thus the side note
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  4. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM