2

I have a function, which adds the given arguments and prints the result.

With integer numbers, there were no problems at all. Used atoi to change string argument -> int.
e.g. : ./main 3 4 5 will print 12.

But if I have ./main 4.5 6 5.5 ?how do I do something like this in C? How can the function "see", that it has to change the argument types now to float?

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

int main( int argc, char* argv[] )
{
   int i , sum = 0;
   for(i=1; i < (argc); ++i)
     sum += atol(argv[i]);
   printf("%d\n", sum);

   return 0;

}
gsamaras
  • 71,951
  • 46
  • 188
  • 305
letter
  • 91
  • 1
  • 7

4 Answers4

3

In , there is no function overloading as in , thus you should use atof, like this:

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

int main( int argc, char* argv[] )
{
   int i;
   double sum = 0;
   for(i = 1; i < (argc); ++i)
     sum += atof(argv[i]);
   printf("%f\n", sum);

   return 0;

}

to treat numbers as reals, not integers.

Output:

gsamaras@gsamaras-A15:~$ ./a.out 4.5 6 5.5
16.000000

since now 6 is treated like 6.0.


You might want to read this as well: How to convert string to float?

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • simply good^^. I forgot to add the false input problem. eg.: ./main 4 4 n 5 It should give a hint, that there is something amiss. Rather than just leaving the n out of the equation. – letter Apr 30 '16 at 19:03
  • @letter you should accept the answer, since it answers your question. :) For what you forgot, just use `isdigit()` (or read this: http://stackoverflow.com/questions/16644906/how-to-check-if-a-string-is-a-number). If you still have problems, post a new question and share the link with me please. :) – gsamaras Apr 30 '16 at 19:13
2

I have tested the code below. It will print the float number upto 2 decimal places.

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

int main( int argc, char* argv[] )
{
    int i;
    double sum = 0;
    for(i=1; i<argc; i++)
        sum += atof(argv[i]);
    printf("%.2f\n", sum);

    return 0;
}
abhiarora
  • 9,743
  • 5
  • 32
  • 57
0

You should use double to store floating point numbers, atof to parse strings and the %f printf specifier.

Roman
  • 36
  • 1
  • @user3078414 [there's no difference for the printf function family](http://stackoverflow.com/questions/25860850/what-is-the-difference-between-f-and-lf-in-c). – Roman Apr 30 '16 at 18:19
0

Although I get an implicit declaration warning for strtod (because the linux manual doesn't tell me the correct includes to use), this code below does work:

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

int main(int argc, char* argv[])
{
  int i;
  double sum=0;
  for(i=1; i < argc; ++i)
sum += strtod(argv[i],NULL);
  printf("%f\n", sum);

  return 0;

}

The manual also states the following as an issue with using atoi():

The atoi() function converts the initial portion of the string pointed to by  nptr  to  int.
The behavior is the same as

strtol(nptr, (char **) NULL, 10);

except that atoi() does not detect errors.
Mike -- No longer here
  • 2,064
  • 1
  • 15
  • 37