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????!!! 4 2759
In article <11************ **********@g49g 2000cwa.googleg roups.com>,
sophie <pa**********@g mail.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,&scann edto,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
"sophie" <pa**********@g mail.com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.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
On 24 Jan 2006 11:32:26 -0800, "sophie" <pa**********@g mail.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("numb er 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');
On Tue, 24 Jan 2006 19:45:34 +0000 (UTC), ro******@ibd.nr c-cnrc.gc.ca
(Walter Roberson) wrote: In article <11************ **********@g49g 2000cwa.googleg roups.com>, sophie <pa**********@g mail.com> wrote:Read in a number as a string: scanf("%s", &number); number = 12345, for arguements sakeprint 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,&scann edto,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.ne t This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 obvious to me, where
I'm going wrong (the compiler messages do not seem to make much sense).
here is the code:
outer class declared as ff in...
|
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 bit-by-bit,
XORing the most significant bit of the FCS with the msb of the
character.
All comments are welcome. Yes, this is for a school assignment. ...
|
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
|
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 value by casting that functions with appropriate data type?
|
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 doesn't write
to x, then it doesnt seem like the compiler could care less whether I
specify the const part. Quite the opposite, if one uses const...
| |
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 * dst=0x01e46130, void * src=0x00001280,
int len=0) + 0x4b bytes C
msvcr80d.dll!_VEC_memcpy(void * dst=0x01e46130, void * src=0x000012fc,
int len=8) + 0x52...
|
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
|
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 *argv)
{
FILE *fp;
int c;
|
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 tables, table 1 called tblProjectDetails, table 2 called tblLearners
The fields in tblProjectDetails are
ProjectPK numeric (Its also the Primary...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |