Whats going on - Char to Int Q:

S

sophie

Whats going on here:

Read in a number as a string:

scanf("%s", &number);

number = 12345, for arguements sake

Print it like this its fine:

printf("number: %s\n", number);


print it like this it will print the first digit, ie '1':

printf("number 0 c: %c\n", number[0]);


print it like this it prints '49' for the 1st char!!! Why?:

printf("number: %d\n", (int)number[0]);


Whats going on, and how do i properly convert a char to an int????!!!
 
W

Walter Roberson

Read in a number as a string:
scanf("%s", &number);
number = 12345, for arguements sake
print it like this it prints '49' for the 1st char!!! Why?:
printf("number: %d\n", (int)number[0]);
Whats going on, and how do i properly convert a char to an int????!!!

You did properly convert the char to an int. The *character* '1' has
integer *value* 49 if you are using ASCII or any of several modern
encodings (but there systems where it is different.)

If you want to extract the integer value 1 from the character '1' then
you need to do one of the following, or equivilent:

if ( sscanf(number, "%d", &numberasint ) < 1 ) {
/* there was an error so do something appropriate */
}

Or
if ( isdigit( number[1] ) ) {
char nb[2];
nb[0] = number[1];
nb[1] = 0;
numberasint = atoi(nb);
} else {
/* there was an error so do something appropriate */
}

Or
{
char nb[2];
char *scannedto;
nb[0] = number[1];
nb[1] = 0;
numberasint = (int) strol(nb,&scannedto,10);
if (scannedto == nb) {
/* there was an error so do something appropriate */
}
}

Or
if ( isdigit( number[1] ) ) {
numberasint = number[1] - '0';
} else {
/* there was an error so do something appropriate */
}
 
F

Fred Kleinschmidt

sophie said:
Whats going on here:

Read in a number as a string:

scanf("%s", &number);

number = 12345, for arguements sake

Print it like this its fine:

printf("number: %s\n", number);


print it like this it will print the first digit, ie '1':

printf("number 0 c: %c\n", number[0]);


print it like this it prints '49' for the 1st char!!! Why?:

printf("number: %d\n", (int)number[0]);


Whats going on, and how do i properly convert a char to an int????!!!

"number" is an array of characters, not a number. Printing the first
character with "%c" format prints the character.
Printing it with "d" format tells prinf to think of the character as an int,
so it prints "49" which is the ASCII code for the character '1'.

To convert to a number, use strtol().
 
B

Barry Schwarz

Whats going on here:

Read in a number as a string:

scanf("%s", &number);

number = 12345, for arguements sake

Print it like this its fine:

printf("number: %s\n", number);


print it like this it will print the first digit, ie '1':

printf("number 0 c: %c\n", number[0]);


print it like this it prints '49' for the 1st char!!! Why?:

printf("number: %d\n", (int)number[0]);


Whats going on, and how do i properly convert a char to an int????!!!

To convert a single character that contains a digit to the
corresponding integer value of that digit, subtract the value '0' from
the character. You last printf would then be (without the useless
cast)

printf("number: %d\n", number[0]-'0');
 
D

Dave Thompson

Read in a number as a string:
scanf("%s", &number);
number = 12345, for arguements sake
print it like this it prints '49' for the 1st char!!! Why?:
printf("number: %d\n", (int)number[0]);
Whats going on, and how do i properly convert a char to an int????!!!

You did properly convert the char to an int. The *character* '1' has
integer *value* 49 if you are using ASCII or any of several modern
encodings (but there systems where it is different.)
Right.

If you want to extract the integer value 1 from the character '1' then
you need to do one of the following, or equivilent:

if ( sscanf(number, "%d", &numberasint ) < 1 ) {
/* there was an error so do something appropriate */
}
Right.

Or
if ( isdigit( number[1] ) ) {
char nb[2];
nb[0] = number[1];
nb[1] = 0;
numberasint = atoi(nb);
} else {
/* there was an error so do something appropriate */
}
That does only the _second_ (?!) digit.
Or
{
char nb[2];
char *scannedto;
nb[0] = number[1];
nb[1] = 0;
numberasint = (int) strol(nb,&scannedto,10);
if (scannedto == nb) {
/* there was an error so do something appropriate */
}
}
Ditto.

Or
if ( isdigit( number[1] ) ) {
numberasint = number[1] - '0';
} else {
/* there was an error so do something appropriate */
}

Ditto but more compactly.

- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top