atof函数在不包含头文件stdlib.h的情况下也能编译运行,但是转换结果是错误的,为什么
来源:互联网 发布:网吧计费系统c语言 编辑:程序博客网 时间:2024/04/29 05:29
c - Not including stdlib.h does not produce any compiler error!
For historical reasons -- specifically, compatibility with very old C programs (pre-C89) -- using a function without having declared it first only provokes a warning from GCC, not an error. But the return type of such a function is assumed to be int, not double, which is why the program executes incorrectly.
If you use -Wall on the command line, you get a diagnostic:
$ gcc -Wall test.c
test.c: In function ‘main’:
test.c:5: warning: implicit declaration of function ‘atoi’
test.c:6: warning: implicit declaration of function ‘atof’
You should use -Wall basically always. Other very useful warning options for new code are -Wextra, -Wstrict-prototypes, -Wmissing-prototypes, -pedantic, and -Wwrite-strings, but compared to -Wall they have much higher false positive rates.
Tangentially: never use atoi nor atof, they hide input errors. Use strtol and strtodinstead.
参考stackoverflow
http://stackoverflow.com/questions/4800102/not-including-stdlib-h-does-not-produce-any-compiler-error
翻译如下:
由于一下历史性的原因,为了兼容老的C程序(早于C89)在使用一个未申明的函数时只会获得一个警告,而不是编译错误。 但是若使用此函数返回值的话,他将被认为是int类型而非double。这也就是为何他会导致程序运行错误。
如果你开启了gcc的 -Wall选项,你会获得如下警告
$ gcc -Wall test.c
test.c: In function ‘main’:
test.c:5: warning: implicit declaration of function ‘atoi’
test.c:6: warning: implicit declaration of function ‘atof’
提示你隐式申明的某些函数。
因此你应该总是打开gcc的 -Wall 选项。 其他有用的警告选项比如 -Wextra, Wstrict-prototypes, -Wmissing-prototypes, -pedantic, 和 -Wwrite-strings. 但是他们和-Wall比起来要严格得多。
另外推荐,不要使用atoi和atof 因为他们并不能处理输入错误 ,使用 strtol 或者 strtod.
- atof函数在不包含头文件stdlib.h的情况下也能编译运行,但是转换结果是错误的,为什么
- c++中,为什么头文件包含了stdlib.h,但是random函数显示有错。
- c++中头文件stdlib.h主要包含的函数
- C++中,头文件stdlib.h主要包含的函数
- stdlib.h包含的函数
- stdlib.h头文件下的常用函数
- c++中stdlib.h头文件中包含哪些函数
- 转:C语言头文件下包含函数(math.h stdio.h stdlib.h string.h)
- stdlib.h头文件
- stdlib.h 头文件
- <stdlib.h> 头文件
- 一些函数的头文件<stdio.h><unistd.h><sys/types.h><stdlib.h><sys/wait.h>头文件的作用
- 指针方面的问题,此程序调试显示是正确的,但是运行起来的结果是错误的为什么?求解
- C++中,头文件stdlib.h主要包含什么函数? C++
- iostream stdlib fstream io.h 头文件的作用
- c语言头文件stdlib.h的详细介绍
- 用Gcc编译包含glib.h头文件的源程序
- stdio.h头文件包含的函数有哪些?
- POJ-2585 Window Pains 【典型处理方法】
- No mapping found for HTTP request with URI
- 统计一篇文章中每个单词出现的次数
- Lucene4.0 BufferedDeletes
- hdu2973威尔逊定理
- atof函数在不包含头文件stdlib.h的情况下也能编译运行,但是转换结果是错误的,为什么
- Jdk与Jre
- ffmpeg 基础教程
- 优秀程序员必须养成的十大习惯
- 商品订购系统时出现的问题汇总 not-null property references a null or transient value 等
- 新组件
- Android开发之媒体扫描详细解析(上)
- 手机系统应用STK 问题总结
- 什么是Qt(二)