C的函数声明和形参

<1>函数声明

最近看C语言,很迷惑,有些地方有函数声明有些地方没有。

查了下教材,做下小结,笔抄写一遍,网上记一遍,备忘!

 

有以下三种情况不用函数声明,可以直接调用:

1.被调函数返回类型为整形或者char型,系统会自理。

举例:

C代码   收藏代码
  1. //代码1  
  2. #include <stdio.h>  
  3. int main(){  
  4.     int a=30;  
  5.     double d=4.4334;  
  6.     printf("a=%d",call(a));  
  7.     return 0;  
  8. }  
  9. int call(int d){  
  10.     printf("HelloWorld\n");  
  11.     return d;  
  12. }  

 正确。如果将call函数的返回类型改为double呢?

Java代码   收藏代码
  1. //代码2  
  2. #include <stdio.h>  
  3. int main(){  
  4.     int a=30;  
  5.     double d=4.4334;  
  6.     printf("d=%f",call(d));  
  7.     return 0;  
  8. }  
  9. double call(double d){  
  10.     printf("HelloWorld\n");  
  11.     return d;  
  12. }  

 gcc表示对call函数的类型很迷惑很纠结~ 

Console代码   收藏代码
  1. D:\prj\core_c>gcc demo.c -o demo.out  
  2. demo.c:8:8: error: conflicting types for 'call'  
  3. demo.c:5:16: note: previous implicit declaration of 'call' was here  

2.被调函数定义在主函数之前。

上面的代码2的call方法在在main之前,就不会有问题了。

3.在定义所有函数之前,先对调用函数做了声明。

Java代码   收藏代码
  1. //代码4  
  2. #include <stdio.h>  
  3. double call(double d);  
  4. int main(){  
  5.     int a=30;  
  6.     double d=4.4334;  
  7.     printf("d=%f",call(d));  
  8.     return 0;  
  9. }  
  10. double call(double d){  
  11.     printf("HelloWorld\n");  
  12.     return d;  
  13. }  

 相当于做了外部声明。

 

<2>形参。

举例:

(1)函数的声明,可以省去变量名。

Java代码   收藏代码
  1. int getMax(int a,int b);  
  2. //可以省去变量名,方便编译系统差错:  
  3. int getMax(int,int);  

(2).宏定义时候的形参不需要类型,不用给它分配内存。 

Java代码   收藏代码
  1. #define P(d) printf("%d",d)  

初学者笔记。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值