473,379 Members | 1,184 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,379 software developers and data experts.

Problem with atoi and strlod

#include<stdio.h>
#include<stdlib.h>

int main()
{
double ans;
ans = strtod("25", NULL);
printf("ans = %d\n",ans);

}

OUTPUT :
ans = 0

EXPECTED OUTPUT
ans = 25

Can anyone please explain this... Else tell me an alternative for
converting string to int.
Jun 27 '08 #1
9 1327
Sanchit wrote:
>
#include<stdio.h>
#include<stdlib.h>

int main()
{
double ans;
ans = strtod("25", NULL);
printf("ans = %d\n",ans);

}

OUTPUT :
ans = 0

EXPECTED OUTPUT
ans = 25

Can anyone please explain this... Else tell me an alternative for
converting string to int.
try %f

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto/
Jun 27 '08 #2
Sanchit <sa************@gmail.comwrites:
#include<stdio.h>
#include<stdlib.h>

int main()
{
double ans;
ans = strtod("25", NULL);
printf("ans = %d\n",ans);

}

OUTPUT :
ans = 0

EXPECTED OUTPUT
ans = 25

Can anyone please explain this... Else tell me an alternative for
converting string to int.
Alternatively you could look in the manual and read what is says about
the functions you are using.

Start with the manual page for strtod and then look at the manual page
for the format specifiers in printf.

Seriously, reading the man pages for the functions is an art in itself
and something you want to try.
Jun 27 '08 #3
Sanchit wrote:
#include<stdio.h>
#include<stdlib.h>

int main()
{
double ans;
ans = strtod("25", NULL);
printf("ans = %d\n",ans);

}

OUTPUT :
ans = 0

EXPECTED OUTPUT
ans = 25

Can anyone please explain this... Else tell me an alternative for
converting string to int.
#include<stdio.h>
#include<stdlib.h>

int main(void)
{
double ans_d;
int ans_i;
printf("The original poster used strtod to interpret the string\n"
"\"25\" as a float. This is OK; strtof and strtold\n"
"would do as well.\n");
ans_d = strtod("25", NULL);
printf("The original poster used \"%%d\" (specifier for an int)\n"
"trying to print a double. This has been changed\n"
"to one of the correct specifiers (\"%%g\")\n"
" ans_d = %g\n\n", ans_d);

printf("Since the desired value is an integer, one might consider\n"
"using one of strtol, strtoll, strtoul, or strtoull"
" instead.\n"
"(C99 also offers strtoimax, strtouimax, and wchar_t"
" versions\n"
"should be available as well.)\n" "Here I use strtol.\n");
ans_i = strtol("25", NULL, 10);
printf(" ans_i = %d\n\n", ans_i);

printf
("In these examples, no use is made of the endpointer that"
" these\n"
"functions return. Nor is any use made of errno.\n"
"These are valuable for any real use of these functions,\n"
"and you should learn to use them.\n");
return 0;

}

The original poster used strtod to interpret the string
"25" as a float. This is OK; strtof and strtold
would do as well.
The original poster used "%d" (specifier for an int)
trying to print a double. This has been changed
to one of the correct specifiers ("%g")
ans_d = 25

Since the desired value is an integer, one might consider
using one of strtol, strtoll, strtoul, or strtoull instead.
(C99 also offers strtoimax, strtouimax, and wchar_t versions
should be available as well.)
Here I use strtol.
ans_i = 25

In these examples, no use is made of the endpointer that these
functions return. Nor is any use made of errno.
These are valuable for any real use of these functions,
and you should learn to use them.
Jun 27 '08 #4
On Sun, 13 Apr 2008 09:50:50 -0700 (PDT), Sanchit
<sa************@gmail.comwrote:
>#include<stdio.h>
#include<stdlib.h>

int main()
{
double ans;
ans = strtod("25", NULL);
printf("ans = %d\n",ans);

}

OUTPUT :
ans = 0

EXPECTED OUTPUT
ans = 25

Can anyone please explain this... Else tell me an alternative for
converting string to int.
You some objection to strtol?
Remove del for email
Jun 27 '08 #5
Barry Schwarz <sc******@dqel.comwrites:
>You some objection to strtol?
You have some objection to verbs?

--
Chris.
Jun 27 '08 #6
Sanchit wrote:
>
#include<stdio.h>
#include <stdio.h>
#include<stdlib.h>
#include <stdlib.h>
>
int main() {
int main(void) {
double ans;
ans = strtod("25", NULL);
printf("ans = %d\n",ans);
printf("ans = %f]\n", abs);
return 0;
}
Obvious program faults/failings marked above.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
** Posted from http://www.teranews.com **
Jun 27 '08 #7
Chris McDonald said:
Barry Schwarz <sc******@dqel.comwrites:
>>You some objection to strtol?

You have some objection to verbs?
Do you have some objection to properly-formed verbs?

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #8
CBFalconer wrote:
Sanchit wrote:
#include<stdio.h>

#include <stdio.h>
#include<stdlib.h>

#include <stdlib.h>
int main() {

int main(void) {
double ans;
ans = strtod("25", NULL);
printf("ans = %d\n",ans);

printf("ans = %f]\n", abs);
ITYM: printf("ans = %f\n", ans);
return 0;
}

Obvious program faults/failings marked above.
Define faults/failings. Only one of your replacements pertains
to an actual error. The other code items are style issues.
[Not that I disagree with your suggested additions.]

--
Peter
Jun 27 '08 #9
Richard Heathfield <rj*@see.sig.invalidwrites:
Chris McDonald said:
>Barry Schwarz <sc******@dqel.comwrites:
>>>You some objection to strtol?

You have some objection to verbs?

Do you have some objection to properly-formed verbs?
Objection sentence fragments?

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #10

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

Similar topics

19
by: Mike Moum | last post by:
I think there may be a bug in string.atoi and string.atol. Here's some output from idle. > Python 2.3.4 (#2, Jan 5 2005, 08:24:51) > on linux2 > Type "copyright", "credits" or "license()"...
6
by: Gaijinco | last post by:
I was trying to solve: acm.uva.es/p/v101/10191.html I did this code: #include <iostream> #include <fstream> #include <string> #include <vector> #include <algorithm> #include <cctype>
34
by: Zulander | last post by:
Hi, have a problem with comparing text in my function for some reason my code get hang at fgets. i am uploading this code in an IC, but i have isolated the problem to fgets(SettingInfo); could...
2
by: useasdf4444 | last post by:
I have the following program: #include <stdio.h> #include <string.h> #include <stdlib.h> void main () { char command; char *com,*pos,*stack1,*stack2; int boxes,boxtable,i,j,k,st1,st2; ...
15
by: vivekian | last post by:
Hi, I have this following class class nodeInfo ; class childInfo { public: int relativeMeshId ;
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.