当前位置:网站首页>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 .
边栏推荐
- [深度学习]激活函数(Sigmoid等)、前向传播、反向传播和梯度优化;optimizer.zero_grad(), loss.backward(), optimizer.step()的作用及原理
- Edge drawing: a combined real-time edge and segment detector
- How do I use Google Chrome 11's Upload Folder feature in my own code?
- AfxMessageBox和MessageBox的用法
- IPv4和IPv6、局域网和广域网、网关、公网IP和私有IP、IP地址、子网掩码、网段、网络号、主机号、网络地址、主机地址以及ip段/数字-如192.168.0.1/24是什么意思?
- Design of serial port receiving data scheme
- [us match preparation] complete introduction to word editing formula
- idea插件备份表
- Md5sum operation
- Listener listener
猜你喜欢

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

Use of comment keyword in database

Feign remote call and getaway gateway

The method to measure the similarity of two vectors: cosine similarity, pytorch calculate cosine similarity: torch nn. CosineSimilarity(dim=1, eps=1e-08)

衡量两个向量相似度的方法:余弦相似度、pytorch 求余弦相似度:torch.nn.CosineSimilarity(dim=1, eps=1e-08)

服务器渲染技术jsp

FCN full Convolution Network Understanding and Code Implementation (from pytorch Official Implementation)

Pytorch training deep learning network settings CUDA specified GPU visible

Introduction to EtherCAT

C语言的sem_t变量类型
随机推荐
Avalanche problem and the use of sentinel
BluePrism注册下载并安装-RPA第一章
Test function in pychram
Database DDL (data definition language) knowledge points
Random seed torch in deep learning manual_ seed(number)、torch. cuda. manual_ seed(number)
还在浪费脑细胞自学吗,这份面试笔记绝对是C站天花板
How do I use Google Chrome 11's Upload Folder feature in my own code?
Subnet division and subnet summary
RSN:Learning to Exploit Long-term Relational Dependencies in Knowledge Graphs
ECMAScript 6.0
Learning notes for introduction to C language multithreaded programming
Kmeans
TEC: Knowledge Graph Embedding with Triple Context
ASGNet论文和代码解读2
过滤器 Filter
AfxMessageBox和MessageBox的用法
4、【WebGIS实战】软件操作篇——数据导入及处理
访问阿里云存储的图片URL实现在网页直接预览略缩图而不直接下载
Pyramid Scene Parsing Network【PSPNet】论文阅读
LeetCode 144二叉树的前序遍历、LeetCode 114二叉树展开为链表