当前位置:网站首页>What happens when a function is called before it is declared in C?
What happens when a function is called before it is declared in C?
2022-07-01 03:37:00 【Three Belle Wenzi】
stay C in , If a function is called before it is declared , The compiler assumes that the return type of the function is int.
for example , The following program failed to compile .
#include <stdio.h>
int main(void)
{
// Note that fun() is not declared
printf("%c\n", fun());
return 0;
}
char fun()
{
return 'G';
}Code compilation results :
prog.c: In function 'main' in :
prog.c:5:17: Warning : function “fun” The implicit statement of [-Wimplicit-function-declaration]
printf("%c\n", fun());
^
prog.c: On the top floor :
prog.c:9:6: error :“ Interesting ” Type conflict of
Character fun ()
^
prog.c:5:17: Be careful : Previously implied “fun” The statement is here
printf("%c\n", fun());If the function in the above code char fun() Is in main() And defined after the call statement , Then it will not compile successfully . Because the compiler assumes by default that the return type is “int”. And in the declaration , If the return type is the same as int Mismatch , Then the compiler will give an error .
The following program is compiled and runs well , Because the function is in the main() As previously defined .
#include <stdio.h>
int fun()
{
return 10;
}
int main(void)
{
// Note the function fun() is declared
printf("%d\n", fun());
return 0;
}Output results :
10What about parameters? ? The compiler makes no assumptions about parameters . therefore , When the function is applied to some parameters , The compiler will not be able to perform compile time checks for parameter types and quantities . This can lead to problems . for example , The following procedure is in GCC Compiled well , The garbage value is generated as output .
#include <stdio.h>
int main (void)
{
printf("%d", sum(10, 5));
return 0;
}
int sum (int b, int c, int a)
{
return (a+b+c);
} The compiler assumes that the input parameters are also int There is such a misunderstanding . If the compiler assumes that the input parameter is int, The above program cannot be compiled .
It is always recommended to declare a function before use , So we won't see anything unexpected when the program is running .
边栏推荐
- Database DDL (data definition language) knowledge points
- Thread data sharing and security -threadlocal
- Split(), split(), slice(), can't you tell?
- RSN:Learning to Exploit Long-term Relational Dependencies in Knowledge Graphs
- [reading notes] copywriting realization -- four golden steps for writing effective copywriting
- IPv4 and IPv6, LAN and WAN, gateway, public IP and private IP, IP address, subnet mask, network segment, network number, host number, network address, host address, and IP segment / number - what does
- Develop industrial Internet with the technical advantages of small programs
- BluePrism注册下载并安装-RPA第一章
- Data exchange JSON
- Design of serial port receiving data scheme
猜你喜欢

Are you still wasting brain cells for self-study? This interview note is definitely the ceiling of station C

Data exchange JSON

完全背包问题

Kmeans
![[us match preparation] complete introduction to word editing formula](/img/e4/5ef19d52cc4ece518e79bf10667ef4.jpg)
[us match preparation] complete introduction to word editing formula

Asgnet paper and code interpretation 2

File upload and download

静态库使用MFC和共享库使用MFC的区别

5、【WebGIS实战】软件操作篇——服务发布及权限管理
![[深度学习]激活函数(Sigmoid等)、前向传播、反向传播和梯度优化;optimizer.zero_grad(), loss.backward(), optimizer.step()的作用及原理](/img/9f/187ca83be1b88630a6c6fbfb0620ed.png)
[深度学习]激活函数(Sigmoid等)、前向传播、反向传播和梯度优化;optimizer.zero_grad(), loss.backward(), optimizer.step()的作用及原理
随机推荐
jeecgboot输出日志,@Slf4j的使用方法
Ctfshow blasting WP
FCN全卷积网络理解及代码实现(来自pytorch官方实现)
torch. histc
ASGNet论文和代码解读2
静态库使用MFC和共享库使用MFC的区别
FCN full Convolution Network Understanding and Code Implementation (from pytorch Official Implementation)
The value of the second servo encoder is linked to the NC virtual axis of Beifu PLC for display
Design of serial port receiving data scheme
4. [WebGIS practice] software operation chapter - data import and processing
Golang multi graph generation gif
打包iso文件的话,怎样使用hybrid格式输出?isohybrid:command not found
E15 solution for cx5120 controlling Huichuan is620n servo error
Depth first traversal of C implementation Diagram -- non recursive code
5. [WebGIS practice] software operation - service release and permission management
后台系统页面左边菜单按钮和右边内容的处理,后台系统页面出现双滚动
Edlines: a real time line segment detector with a false detection control
Thread data sharing and security -threadlocal
Include() of array
终极套娃 2.0 | 云原生交付的封装