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的更多相关文章

  1. [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++编译器是如何区分不同的函数的呢?----是 ...

  2. [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中有可能可以. ...

  3. Js中有关变量声明和函数声明提升的问题

    在ECMAScript5中没有块级作用域一说,只有函数作用域和全局作用域,在其中声明的变量和函数和其他语言的展现形式不同,在某些情况下不一定需要先定义后使用,函数和变量的使用可以在其声明之前,这到底是 ...

  4. [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++中有不同的变量 ...

  5. C++中全局变量的声明和定义

    原文链接:http://blog.csdn.net/candyliuxj/article/details/7853938 (1)编译单元(模块) 在VC或VS上编写完代码,点击编译按钮准备生成exe文 ...

  6. 关于C语言中的结构体内嵌函数(转)

    https://blog.csdn.net/qq_39490500/article/details/80457831 看门见山 1.内嵌函数定义举例:经过真实测试 在函数中声明定义结构体 #inclu ...

  7. 【C#小知识】C#中一些易混淆概念总结(七)---------解析抽象类,抽象方法

    目录: [C#小知识]C#中一些易混淆概念总结--------数据类型存储位置,方法调用,out和ref参数的使用 [C#小知识]C#中一些易混淆概念总结(二)--------构造函数,this关键字 ...

  8. 《Visual C++ 2010入门教程》系列四:VC2010中初学者常见错误、警告和问题

    <Visual C++ 2010入门教程>系列四:VC2010中初学者常见错误.警告和问题   这一章将帮助大家解释一些常见的错误.警告和问题,帮助大家去理解和解决一些常见问题,并了解它的 ...

  9. Java基础扫盲系列(-)—— String中的format

    Java基础扫盲系列(-)-- String中的format 以前大学学习C语言时,有函数printf,能够按照格式打印输出的内容.但是工作后使用Java,也没有遇到过格式打印的需求,今天遇到项目代码 ...

随机推荐

  1. c 语言时间的输出和比较

    time_t The most basic representation of a date and time is the type time_t. The value of a time_t va ...

  2. Android实现Http协议案例

    在Android开发中,使用Http协议实现网络之间的通信是随处可见的,使用http方式主要采用2中请求方式即get和post两种方式. 一.使用get方式: HttpGet httpGet = ne ...

  3. [Guava源码分析] Preconditions 前置条件

    我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3874170.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...

  4. IPC with pipes, demo of 'popen'

    #include <stdio.h> #include <unistd.h> int main() { FILE* stream = popen ("sort&quo ...

  5. Win8 URI 方案 ms-appX 用法大全

    ms-appdata://可以引用来自应用的本地.漫游和临时数据文件夹中的应用文件ms-appdata:///local/hello/logo.png ms-appx://可以引用来自应用包的应用文件 ...

  6. Linux mkisofs 创建光盘镜像文件(Linux指令学习笔记)

    mkisofs命令 创建光盘文件的系统的命令是mkisofs.光盘系统有多种格式,利用Linux系统提供的光盘文件系统创建 命令mkisofs,可以创建多种iso9660文件系统. 我们一般不用mki ...

  7. JQuery之proxy实现绑定代理

    在javascript中,this指代的对象时常会变化,这会造成程序,混乱,一般做法就是先将this保存在一个变量中,就不怕她变了,我们先看一个小例子 var A = function(){ this ...

  8. javascript里面技巧整理

    web develop tools secrets: http://jinlong.github.io/blog/2013/08/29/devtoolsecrets/ 1.Date new Date( ...

  9. C# Winform中DataGridView的DataGridViewCheckBoxColumn使用方法

    下面介绍Winform中DataGridView的DataGridViewCheckBoxColumn使用方法: DataGridViewCheckBoxColumn CheckBox是否选中 在判断 ...

  10. ubuntu下安装phpstudy环境记录

    下载一键安装包 下载地址:http://www.phpstudy.net/a.php/208.html 安装过程 开启终端 更改文件权限 chmod +x phpstudy 进行安装 ./phpstu ...