473,396 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Whats going on - Char to Int Q:

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????!!!

Jan 24 '06 #1
4 2748
In article <11**********************@g49g2000cwa.googlegroups .com>,
sophie <pa**********@gmail.com> wrote:
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 */
}
--
If you lie to the compiler, it will get its revenge. -- Henry Spencer
Jan 24 '06 #2

"sophie" <pa**********@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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().

--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project

Jan 24 '06 #3
On 24 Jan 2006 11:32:26 -0800, "sophie" <pa**********@gmail.com>
wrote:
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');
Feb 4 '06 #4
On Tue, 24 Jan 2006 19:45:34 +0000 (UTC), ro******@ibd.nrc-cnrc.gc.ca
(Walter Roberson) wrote:
In article <11**********************@g49g2000cwa.googlegroups .com>,
sophie <pa**********@gmail.com> wrote:
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
Feb 6 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Alfonso Morra | last post by:
I have a class that contains a nested class. The outer class is called outer, and the nested class is called inner. When I try to compile the following code, I get a number of errors. It is not...
1
by: Matthew Wilson | last post by:
I need to write a function crc(msg, len) that gets a char array of length len and then calculates the crc32 for the code. I don't understand what's going wrong in the code I have. It goes...
6
by: thesushant | last post by:
hi, whats the use of third argument to main( ), i.e. environmental parameters.... if i am not wrong ? 1st 1 is argc 2nd is argv and what bout the 3rd 1??????????? sushant
5
by: kernel.lover | last post by:
hello, I want to know if a fuction say malloc is declared as void *malloc() then whats the significance of void here. Does void * is used when function has the flexibility to return any type of...
20
by: Snis Pilbor | last post by:
Whats the point of making functions which take arguments of a form like "const char *x"? It appears that this has no effect on the function actually working and doing its job, ie, if the function...
2
by: abubakarm | last post by:
Hi, There is a strang bug appearing not-so-often in my application. I'm pasting the stack calls window contents when my app crashes. Following it is: msvcr80d.dll!fastcopy_I(void *...
5
by: sam | last post by:
HI, Whats the meaning of this code:- char buff; memset(buff, 'A' , 100); sometimes we can place hex value in place of 'A' like 0x90c but how this code function actually. thanks in advance
7
by: Adrian | last post by:
What is the best was to do this in c++. This is going to be used for unix util that should be able to have input piped to it or file name spec #include <stdio.h> int main(int argc, char...
12
by: azalea45 | last post by:
Hi all I am a newbie when it comes to access. My company requires that all Databases run on the company SQL back-end as a result i have a Access project that connects to the server. I have two...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.