Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why is strtof is always evaluating to HUGE_VAL?

Tags:

c

What could be the issue here? It doesn't matter what number I choose for str, it is always 26815615859885194199148049996411692254958731641184786755447122887443528060147093953603748596333806855380063716372972101707507765623893139892867298012168192.00

char *str = "2.6";
printf("%f\n", strtof(str, (char**)NULL));
//prints 26815615859885194199148049996411692254958731641184786755447122887443528060147093953603748596333806855380063716372972101707507765623893139892867298012168192.00

whole program:

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

int main(int argc, char *argv[])
{
    char *str = "2.6";
    printf("%f\n", strtof(str, NULL));
    return 1;
}

compile with -Wall:

test4.c:7: warning: implicit declaration of function âstrtofâ
like image 323
user318747 Avatar asked Dec 01 '22 10:12

user318747


1 Answers

What platform are you building for/on? The warning that you say is being emitted:

test4.c:7: warning: implicit declaration of function âstrtofâ

indicates that the compiler doesn't know that strtof() returns a float, so it's going to push an int to the printf() call instead of a double. strtof() is normally declared in stdlib.h, which you're including. But it wasn't a standard function until C99, so the exact compiler platform (and configuration/options you're using) may affect whether it's being made available or not.

like image 88
Michael Burr Avatar answered Dec 15 '22 04:12

Michael Burr