Thread: (char)09 not valid?

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    27

    (char)09 not valid?

    ok, if I do the following, I get the following:
    for some reason the compiler thinks that 08 and 09 are octal values. Anything decimal 10+ is fine.

    Code:
    outp<<(char)09
    
    gives
    
    invalid digit "9" in octal constant|
    How do I tell the compiler it's a decimal?

    If I do this, it is fine!
    Code:
    int main()
    {
          for ( int x = 0; x < 20; x++ ) {
              cout<< x <<": "<< (char)x <<"\n";
              //Note the use of the int version of x to
              // output a number and the use of (char) to
              // typecast the x into a character
              // which outputs the ASCII character that
              // corresponds to the current number
          }
      cin.get();
        return 0;
    }

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    How do I tell the compiler it's a decimal?
    Code:
    outp << (char) 9;
    The reason compiler "thinks" they are octal is because literals beginning with a leading 0 are octal, like literals beginning with 0x are hex.
    Last edited by anon; 11-25-2009 at 05:15 PM.
    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
    Registered User
    Join Date
    Nov 2009
    Posts
    27
    ah cool, thank you, I wonder if this point could be added to the C++ tutorial on typecasting?

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by mikeyp View Post
    ah cool, thank you, I wonder if this point could be added to the C++ tutorial on typecasting?
    It has nothing to do with typecasting. The compiler would complain the same way if the cast had not been there.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    27
    Quote Originally Posted by brewbuck View Post
    It has nothing to do with typecasting. The compiler would complain the same way if the cast had not been there.
    I know I'm new to C++ programming but can you explain what you mean? My request to have it included in the tutorial is based on the below, where I got my code for the char conversion from.

    Cprogramming.com Tutorial: Typecasting
    Second code example down... the tutorial needs the different types of code (dec, oct, hex) explaining to save confusion in the future.

    My point was that this tutorial would be a good time to explain this.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Typecasting has little to do with how compilers parse literals.

    We usually write 9 as 9 and not 09.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Minimum valid address value
    By BMintern in forum C Programming
    Replies: 6
    Last Post: 04-12-2008, 08:21 AM
  2. Replies: 9
    Last Post: 07-26-2007, 12:03 PM
  3. recursion error
    By cchallenged in forum C Programming
    Replies: 2
    Last Post: 12-18-2006, 09:15 AM
  4. Are these valid operators
    By Morgan in forum C Programming
    Replies: 3
    Last Post: 08-12-2003, 11:56 AM
  5. Azbia - a simple RPG game code
    By Unregistered in forum Game Programming
    Replies: 11
    Last Post: 05-03-2002, 06:59 PM