Simple problem?

S

Shel Sherman

What's wrong with this code? Using Gcc in Fedora 4 it gives the wrong value
for x unless it is declared a float.

#include <stdio.h>
int main{
char * estr = "1.234";
double x = 0;
sscanf(estr, "%g", &x);
printf("%g", x);
return 0;
}

typical result is x = -0.0140015.

Thanks,
Shel Sherman
 
M

Martin Ambuhl

Shel said:
What's wrong with this code? Using Gcc in Fedora 4 it gives the wrong value
for x unless it is declared a float.

#include <stdio.h>
int main{
char * estr = "1.234";
double x = 0;
sscanf(estr, "%g", &x);
printf("%g", x);
return 0;
}

typical result is x = -0.0140015.

After fixing the three obvious errors in your code, yielding:

#include <stdio.h>
int main(void)
{ /* mha: fixed missing parameter list */
char *estr = "1.234";
double x = 0;
sscanf(estr, "%lg", &x); /* mha: fixed format specifier */
printf("%g\n", x); /* mha: added EOL character */
return 0;
}

I get as output:

1.234
 

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,797
Messages
2,569,647
Members
45,380
Latest member
LatonyaEde

Latest Threads

Top