Wierd output with a command line argument

O

Oscarian

I'm curious to know if anyone can explain why the following code does not
return the second argument exactly, but rather appears to return a random
number (at least it does that on my box).

#include <stdio.h>

int main (int argc, char *argv[]) {
/* Make sure you enter a numerical second argument */
if (argc != 2)
return (0);
printf ("%f\n", atof(argv[1]));
return (0);
}

-Oscarian
 
S

santosh

Oscarian said:
I'm curious to know if anyone can explain why the following code does not
return the second argument exactly, but rather appears to return a random
number (at least it does that on my box).

#include <stdio.h>

int main (int argc, char *argv[]) {
/* Make sure you enter a numerical second argument */
if (argc != 2)
return (0);
printf ("%f\n", atof(argv[1]));
return (0);
}

Include stdlib.h for supplying the prototype for atof, otherwise the
compiler assumes that it returns an int value.

Also consider using strtod which is bit more robust on bad arguments.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top