[C/CPP系列知识] 在C中使用没有声明的函数时将发生什么 What happens when a function is called before its declaration in C
http://www.geeksforgeeks.org/g-fact-95/
1 在C语言中,如果函数在声明之前被调用,那么编译器假设函数的返回值的类型为INT型,
所以下面的code将无法通过编译:
#include <stdio.h>
int main(void)
{
// Note that fun() is not declared
printf("%d\n", fun());
return ;
} char fun()
{
return 'G';
}
错误:其实就是fun函数定义了两遍,冲突了
test1.c::: error: conflicting types for 'fun'
char fun()
^
test1.c::: note: previous implicit declaration of 'fun' was here
printf("%d\n", fun());
^
将返回值改成int行可以编译并运行:
#include <stdio.h>
int main(void)
{
printf("%d\n", fun());
return ;
} int fun()
{
return ;
}
2 在C语言中,如果函数在声明之前被调用,如果对函数的参数做检测?
compiler assumes nothing about parameters. Therefore, the compiler will not be able to perform compile-time checking of argument types and arity when the function is applied to some arguments. This can cause problems.
编译器对参数不做任何假设,所以无法做类型检查。 下面code就会有问题,输出是garbage
#include <stdio.h> int main (void)
{
printf("%d", sum(, ));
return ;
}
int sum (int b, int c, int a)
{
return (a+b+c);
}
输出结果:
diego@ubuntu:~/myProg/geeks4geeks/cpp$ ./a.out diego@ubuntu:~/myProg/geeks4geeks/cpp$ ./a.out diego@ubuntu:~/myProg/geeks4geeks/cpp$ ./a.out
-
diego@ubuntu:~/myProg/geeks4geeks/cpp$ ./a.out diego@ubuntu:~/myProg/geeks4geeks/cpp$ ./a.out
-
diego@ubuntu:~/myProg/geeks4geeks/cpp$ ./a.out
-
diego@ubuntu:~/myProg/geeks4geeks/cpp$ ./a.out diego@ubuntu:~/myProg/geeks4geeks/cpp$
所以It is always recommended to declare a function before its use so that we don’t see any surprises when the program is run (See thisfor more details).
[C/CPP系列知识] 在C中使用没有声明的函数时将发生什么 What happens when a function is called before its declaration in C的更多相关文章
- [C/CPP系列知识] C++中extern “C” name mangling -- Name Mangling and extern “C” in C++
http://www.geeksforgeeks.org/extern-c-in-c/ C++函数重载(function overloading),但是C++编译器是如何区分不同的函数的呢?----是 ...
- [C/CPP系列知识] 那些程序C语言可以编译通过但C++无法编译成功 Write a C program that won’t compile in C++
http://www.geeksforgeeks.org/write-c-program-wont-compiler-c/ 1) C++中在函数声明之前调用一个函数会引发错误,但是在C中有可能可以. ...
- Js中有关变量声明和函数声明提升的问题
在ECMAScript5中没有块级作用域一说,只有函数作用域和全局作用域,在其中声明的变量和函数和其他语言的展现形式不同,在某些情况下不一定需要先定义后使用,函数和变量的使用可以在其声明之前,这到底是 ...
- [C/CPP系列知识] Type difference of character literals 和 bool in C and C++
C/C+中的每一个常亮(every literal)都是有类型的,例如10 就是int型的,因此siziof(10)和sizeof(int)是相同的,但是字符型常亮(‘a’)在C和C++中有不同的变量 ...
- C++中全局变量的声明和定义
原文链接:http://blog.csdn.net/candyliuxj/article/details/7853938 (1)编译单元(模块) 在VC或VS上编写完代码,点击编译按钮准备生成exe文 ...
- 关于C语言中的结构体内嵌函数(转)
https://blog.csdn.net/qq_39490500/article/details/80457831 看门见山 1.内嵌函数定义举例:经过真实测试 在函数中声明定义结构体 #inclu ...
- 【C#小知识】C#中一些易混淆概念总结(七)---------解析抽象类,抽象方法
目录: [C#小知识]C#中一些易混淆概念总结--------数据类型存储位置,方法调用,out和ref参数的使用 [C#小知识]C#中一些易混淆概念总结(二)--------构造函数,this关键字 ...
- 《Visual C++ 2010入门教程》系列四:VC2010中初学者常见错误、警告和问题
<Visual C++ 2010入门教程>系列四:VC2010中初学者常见错误.警告和问题 这一章将帮助大家解释一些常见的错误.警告和问题,帮助大家去理解和解决一些常见问题,并了解它的 ...
- Java基础扫盲系列(-)—— String中的format
Java基础扫盲系列(-)-- String中的format 以前大学学习C语言时,有函数printf,能够按照格式打印输出的内容.但是工作后使用Java,也没有遇到过格式打印的需求,今天遇到项目代码 ...
随机推荐
- iOS 父子关系
1.面向对象特征,类的继承 成员变量(实例变量) 子类继承父类所有功能,只能直接(访问)调用父类中的.h中的protect和public成员变量(实例变量)及方法, .h中的私有的成员变量,子类不能直 ...
- TableViewCell自适应高度
//初始化TableView时设置 self.tv.estimatedRowHeight=54;self.tv.rowHeight=UITableViewAutomaticDimension;
- Cocos2d-x场景切换相关函数介绍
场景切换是通过导演类Director实现的,其中的相关函数如下: runWithScene(Scene* scene).该函数可以运行场景.只能在启动第一个场景时候调用该函数.如果已经有一个场景运行情 ...
- c 语言时间的输出和比较
time_t The most basic representation of a date and time is the type time_t. The value of a time_t va ...
- 7款经典炫酷的HTML5/jQuery动画应用示例及源码
jQuery是一款普遍受前端开发者欢迎的Javascript框架,但是开发者貌似更关注jQuery开发的插件,海量的jQuery插件让前端开发者非常方便.HTML5的加入让jQuery这个家族更加丰富 ...
- spring IOC经典理解
不多解释,直接上图片!
- 济南学习 Day 4 T2 am
LYK 与实验室(lab)Time Limit:5000ms Memory Limit:64MB题目描述LYK 在一幢大楼里,这幢大楼共有 n 层,LYK 初始时在第 a 层上.这幢大楼有一个秘密实验 ...
- IC卡的逻辑卡号和市民卡卡号
CPU卡,IC卡等的卡号与卡在出厂时的卡号.
- file与 byte[] 互转
byte 转file String filepath="D:\\"+getName(); File file=new File(filepath); ...
- 【原】WinForm中的DataGridView加入Combbox或者DropDownButton后,操作变慢
DataGridView里加入了DropDownButto列,加载数据后点击这一列,反应很慢;要点击三到四次才会展示下拉列表; 原因是DataGridView的EditMode设置问题; 将DataG ...