-3

I have a problem. I am using this code to print from text file but the program gives me a different number -such as 11732408.000000- each time. However I don't get this problem when ex is integer.

#include <stdio.h>
#include <string.h>

int main() {
    char example[] ="123.12/456 ";
    double ex = atof(strtok(example, "/"));
    printf("%lf", ex);
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
aybkcn
  • 25
  • 4
  • 1
    Welcome to SO. Please provide examples of working and non-working input. – rtx13 Apr 12 '20 at 23:31
  • 4
    Please provide a [minimal verifiable example](https://stackoverflow.com/help/minimal-reproducible-example). That is, code that others can copy, run and reproduce the problem. Please ensure to include the exact input, expected result and actual result. – kaylum Apr 12 '20 at 23:37
  • I cannot exactly understand what you mean. – aybkcn Apr 12 '20 at 23:44
  • 2
    We can't run your code because it is incomplete. And we also don't know what is in your input file and so can't even check that the code is reading it correctly. And finally, you have given an incorrect output but we don't know what the expected correct output is. Please read the provided link on how to create a minimal verifiable example. – kaylum Apr 12 '20 at 23:45
  • I need to get some numbers such as 44.5676. If I change from float to int I can get it but I need to get in double type. – aybkcn Apr 12 '20 at 23:45
  • 1
    Yes, but what does the input file look like? How is `line` declared? How was the file read into `line`? What is the code before this? In programming, the details really matter alot. The error could be in any of those steps. Thus with so much missing detail we can't effectively help. – kaylum Apr 12 '20 at 23:47
  • Thank you for your advice about a good question. This is my first question and English is not my native language so I may not have been able to explain my problem completely. I edited the question and elaborated it, I hope it is understandable. – aybkcn Apr 12 '20 at 23:58
  • the posted code does not compile! it is missing a LOT, including the `#include` statements for the needed header files, a `main()` function AND the complete function that contains this code snippet – user3629249 Apr 13 '20 at 01:20
  • when using `strtok()`, always check (!=NULL) before trying to use the pointer returned by `strtok()` – user3629249 Apr 13 '20 at 01:21
  • 1
    the functions: `atoi()` and `atof()` do not step over leading spaces, so should be checking for that condition. The function `strtok()` tends to skip over multiple/adjacent 'delimiters` so should watch for that kind of 'oops'. Suggest (for debugging) to print each char array that is returned by `strtok()` so you will know exactly what is happening – user3629249 Apr 13 '20 at 01:31
  • [`atoi` shouldn't be used](https://stackoverflow.com/q/17710018/995714) (at least on non trusted input like files). Use `strtol` instead – phuclv Apr 13 '20 at 02:25
  • Thanks for your comments. I edited the question by simplifying it, I hope you can help. – aybkcn Apr 13 '20 at 07:16

2 Answers2

0

I could solve my problem. Thank you for your helps.

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

int main ()
{

    char example[20]  ="123.12/456 ";
    double ex=atof(strtok(example,"/"));
    printf("%lf",ex);




    return 0;
}
aybkcn
  • 25
  • 4
0

You forgot to include <stdlib.h> which contains the declaration of atof().

Your compiler is lenient and accepts your code is spite of the missing declaration, and it incorrectly infers the prototype to be int atof(char *), which causes undefined behavior when storing the return value to ex.

Hence the bogus output.

Note also that the l in the format %lf is necessary for scanf() but ignored by printf() as float arguments are implicitly converted to double when passed to vararg functions.

Here is a corrected version:

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

int main() {
    char example[] = "123.12/456 ";
    char *p = strtok(example, "/");
    if (p != NULL) {
       double ex = atof(p);
       printf("%f\n", ex);
    }
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189