当前位置:网站首页>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 .
边栏推荐
- Md5sum operation
- 家居网购项目
- Basic concepts of database
- 在 C 中声明函数之前调用函数会发生什么?
- C#实现基于广度优先BFS求解无权图最短路径----完整程序展示
- 衡量两个向量相似度的方法:余弦相似度、pytorch 求余弦相似度:torch.nn.CosineSimilarity(dim=1, eps=1e-08)
- Random seed torch in deep learning manual_ seed(number)、torch. cuda. manual_ seed(number)
- Keil5中如何做到 0 Error(s), 0 Warning(s).
- torch. histc
- The value of the second servo encoder is linked to the NC virtual axis of Beifu PLC for display
猜你喜欢

Server rendering technology JSP

FCN全卷积网络理解及代码实现(来自pytorch官方实现)

后台系统页面左边菜单按钮和右边内容的处理,后台系统页面出现双滚动

在线公网安备案保姆级教程【伸手党福利】

Edge Drawing: A combined real-time edge and segment detector 翻译

Edge drawing: a combined real-time edge and segment detector

Test function in pychram

pytorch训练深度学习网络设置cuda指定的GPU可见
![[deep learning] activation function (sigmoid, etc.), forward propagation, back propagation and gradient optimization; optimizer. zero_ grad(), loss. backward(), optimizer. Function and principle of st](/img/9f/187ca83be1b88630a6c6fbfb0620ed.png)
[deep learning] activation function (sigmoid, etc.), forward propagation, back propagation and gradient optimization; optimizer. zero_ grad(), loss. backward(), optimizer. Function and principle of st

Leetcode 128 longest continuous sequence (hash set)
随机推荐
Nacos
Md5sum operation
后台系统右边内容如何出现滚动条和解决双滚动条的问题
go实现命令行的工具cli
[reading notes] copywriting realization -- four golden steps for writing effective copywriting
详解Spark运行模式(local+standalone+yarn)
4、【WebGIS实战】软件操作篇——数据导入及处理
Leetcode:剑指 Offer 59 - I. 滑动窗口的最大值
RSN:Learning to Exploit Long-term Relational Dependencies in Knowledge Graphs
leetcode 1818 绝对值,排序,二分法,最大值
Bilinear upsampling and f.upsample in pytorch_ bilinear
JUC学习
gcc使用、Makefile总结
[深度学习]激活函数(Sigmoid等)、前向传播、反向传播和梯度优化;optimizer.zero_grad(), loss.backward(), optimizer.step()的作用及原理
[us match preparation] complete introduction to word editing formula
How do I use Google Chrome 11's Upload Folder feature in my own code?
[小样本分割]论文解读Prior Guided Feature Enrichment Network for Few-Shot Segmentation
力扣-两数之和
shell脚本使用两个横杠接收外部参数
Subnet division and subnet summary