c - Difference between "implicit declaration of function" and the original version of the function -
i use gcc4.8. , wrote such code, using sleep.
int main(int argc, char *argv[]) { /* know it's worong pass floating number sleep * testing. */ sleep(0.001); return 0; } i compile "gcc -wall a.c -o a", got warning "implicit declaration of function ‘sleep’ [-wimplicit-function-declaration]". ran it, program sleeps approximately 1 second (it seems sleep ceils 0.001 1).
then change code this:
#include <unistd.h> /* add header file */ int main(int argc, char *argv[]) { /* know it's worong pass floating number sleep * testing. */ sleep(0.001); return 0; } this time sleeps 0 second, seems sleep floors 0.001 0.
shouldn't these 2 sleep identical?
in first (wrong) case real floating point value given sleep assumed prototype of sleep takes floating point value (a double actually). sleep interpret bit representation of double int , wait many seconds. lucky 1 second. in second case floating point value cast int rounding 0.
Comments
Post a Comment