Thread: assigning char value to an int

  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,815
    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%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 02: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