Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

convert from char to integer value

Status
Not open for further replies.

souphmars

Technical User
Oct 28, 2004
34
US
i input as a char-how do i change the character which is a number to it its value so i can add it
 
In C/C++ char type is an integral (i.e. integer) type. You can add/subtract chars as integers, for example:
Code:
char ch = '2';
int  n;
...
n = ch - '0'; /* n == 2 now */
Be careful: there are three char types in these languages:
1. signed char
2. unsigned char
3. char /* as is: may be treated as signed or unsigned */
For ANSI codes (0..127) is no matter what's a simple char treatment in the implementation. You may use explicit cast to (unsigned char) in proper cases (more portability)...
 
I think what you're talking about refers more specifically to this. If the user inputs a string (in an editbox) that is "1234", to get the actual integer value, use the function atoi(). That converts a character string to an integer.

bdiamond
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top