| | |
pls help me to convert char to integer.
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2005
Posts: 17
Reputation:
Solved Threads: 0
I have a problem in converting char to integer. My code is as follow:
char lang [2][4] ={"en","fr"};
char ch = lang[0][0];
int i = sprintf("%d",ch); //want to get integer value of 'e' in string "en"
ch = lang[0][1];
int j = sprintf("%d",ch);
The error is "sprintf cannot accept parameter 2 from const char*to char".
pls help me.
Thanks.
Rgds,
su
char lang [2][4] ={"en","fr"};
char ch = lang[0][0];
int i = sprintf("%d",ch); //want to get integer value of 'e' in string "en"
ch = lang[0][1];
int j = sprintf("%d",ch);
The error is "sprintf cannot accept parameter 2 from const char*to char".
pls help me.
Thanks.
Rgds,
su
•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
I think it's:
Probably better ways, but that's how I do it.
C Syntax (Toggle Plain Text)
int x = atoi(char);
Probably better ways, but that's how I do it.
Getting the numeric value of a single character is simple:
You're confused with the distinction between value and representation. The values of lang[0][0] and i are identical. The only difference is how they're printed, much like octal, decimal, and hexadecimal. The values are the same, but the representation is different. That's why you can do this:
>int i = sprintf("%d",ch);
This is not how sprintf works. Until you have a stronger foundation in the idioms of C, you should refrain from guessing and use a good reference.
>int x = atoi(char);
That's broken, and you should know better. atoi takes a null terminated string. If you pass it a single character that doesn't have the value of 0, you invoke undefined behavior. Not to mention that atoi will fail and return 0 if the character is non-numeric, which in my world, 'e' most certainly is.
Here's a nice little guideline for both of you: If you don't know, go find out. In C and C++ there's no margin for error, so a guess is just asking for trouble.
C Syntax (Toggle Plain Text)
char lang[2][4] = {"en","fr"}; int i = lang[0][0];
C Syntax (Toggle Plain Text)
printf ( "%c\n", i ); /* Print i as a character */ printf ( "%d\n", i ); /* Print i as an integer */
This is not how sprintf works. Until you have a stronger foundation in the idioms of C, you should refrain from guessing and use a good reference.
>int x = atoi(char);
That's broken, and you should know better. atoi takes a null terminated string. If you pass it a single character that doesn't have the value of 0, you invoke undefined behavior. Not to mention that atoi will fail and return 0 if the character is non-numeric, which in my world, 'e' most certainly is.
Here's a nice little guideline for both of you: If you don't know, go find out. In C and C++ there's no margin for error, so a guess is just asking for trouble.
New members chased away this month: 4
![]() |
Similar Threads
- Pls help :Find first non-repeated char in a string (C++)
- How to LPCTSTR Convert to char * (C++)
- how to convert char to integer??? (C++)
Other Threads in the C Forum
- Previous Thread: c and shared libs ?? how to do it right?
- Next Thread: Design and Implement a class hierarchy for an organization/university
Views: 21391 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays bash binarysearch centimeter char convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory drawing dynamic executable fflush file floatingpointvalidation fork frequency getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling hardware highest homework i/o ide inches infiniteloop initialization interest kilometer lazy linked linkedlist linux linuxsegmentationfault list logical_drives match matrix meter microsoft motherboard multi mysql open opendocumentformat openwebfoundation pattern pause pdf pointer pointers posix power problem program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling segmentationfault send shape single socketprograming socketprogramming spoonfeeding stack standard strchr string strings structures student suggestions system test testautomation unix urboc user voidmain() win32 win32api windows.h






