assigning char value to an int

This is a discussion on assigning char value to an int within the C++ Programming forums, part of the General Programming Boards category; Code: #include<iostream.h> int main() { int a; cin>>a; cout<<a; } hey guys, i had the idea that when a character ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    91

    assigning char value to an int

    Code:
    #include<iostream.h>
    
    int main()
    {
    	int a;
    	cin>>a;
    	cout<<a;
    	
    }
    hey guys,
    i had the idea that when a character is assigned to an int variable,
    what would be stored in the int variable would be the ascii value of the character.

    so in the above code,if i input 'a' when the program runs, i thought 97
    will be the output but its not so

    please explain or show me somewhere i can read about htis

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,260
    Quote Originally Posted by theju112 View Post
    i had the idea that when a character is assigned to an int variable,
    what would be stored in the int variable would be the ascii value of the character.
    That is completely wrong.

    When reading an int using formatted I/O, the digits 0-9 and sign characters (+/-) are interpreted in order to get the value. So "+152" will result in an int getting the value 152. If other characters are encountered, reading stops, and that character is left in the stream.

    If doing a formatted read of an integer in other bases, the same idea applies, except the range of characters interprets as digits changes (octal 0-8, hex 0-9 and A-E or a-e).

    If you want input 'a' to get the value 97, then make a of type char. Then the character is read directly from the stream, not interpreted in terms of formatted digits. (This assuming your compiler implementation works with ASCII characters, which is common but not actually guaranteed).
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Kathmandu
    Posts
    4
    You cannot assign character to an integer variable.
    For more information visit: Input and Output in C

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    Quote Originally Posted by programiz
    You cannot assign character to an integer variable.
    That is not true.

    Quote Originally Posted by programiz
    For more information visit: Input and Output in C
    It looks like you are linking to your own resource. That can be fine, except that the page that you linked to is concerned with C, yet this is C++. Now, C I/O can be used in C++, but theju112's post is not about that, so your link is irrelevant.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-08-2009, 01:47 AM
  2. char *, accessing/assigning problem
    By Extrovert in forum C++ Programming
    Replies: 4
    Last Post: 07-15-2005, 03:33 AM
  3. Assigning a value to a char
    By super_stripey in forum C Programming
    Replies: 5
    Last Post: 10-08-2004, 01:00 PM
  4. Assigning Const Char*s, Char*s, and Char[]s to wach other
    By Inquirer in forum Linux Programming
    Replies: 1
    Last Post: 04-29-2003, 10:52 PM
  5. assigning and casting (char) to (int)
    By matheo917 in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2002, 12:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21