0
int main(){

    double N = 0;
    char unit[10];
    printf("Insert float > ");
    scanf("%f%s",&N,unit);
    printf("\n\n%f\n\n",N);

}

So in the terminal:

Insert float > 0.2hz

output:

0.000000hz

How could I save in N value the correct 0.2 value?

Allexj
  • 1,375
  • 6
  • 14
  • 29
  • 2
    `N` is a `double`, not a `float`. – mch Jan 19 '21 at 09:42
  • 2
    Please compile your code using a basic set of compilation flags to catch simple errors like this -- any decent compiler will warn you about this. See your compiler's documentation (e.g. for [`gcc`](https://gcc.gnu.org/onlinedocs/gcc/Option-Summary.html), you might use `gcc -Wall -Werror -Wextra program.c`). Also, please get familiar with the documentation for `printf` and in particular, the correct format specifiers to use. – costaparas Jan 19 '21 at 09:45
  • 3
    `%f` -> `%lf` should do the job – Jabberwocky Jan 19 '21 at 09:45
  • 2
    That cannot possibly be the actual output of your code – klutt Jan 19 '21 at 09:49
  • @klutt true, it doesn't print `unit`, but the OP could be running a different version (which often happens on SO..) – costaparas Jan 19 '21 at 09:51
  • Side note: **always check the return value of `scanf`** -- its very important. – costaparas Jan 19 '21 at 09:54

0 Answers0