Problem with atoi and strlod

S

Sanchit

#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.
 
R

Richard

Sanchit said:
#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.
 
M

Martin Ambuhl

Sanchit said:
#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.
 
B

Barry Schwarz

#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
 
P

Peter Nilsson

CBFalconer said:
Sanchit said:
#include<stdio.h>

#include said:
#include<stdlib.h>

#include said:
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.]
 

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,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top