C的函式宣告和形參

來源:互聯網
上載者:User

<1>函式宣告

最近看C語言,很迷惑,有些地方有函式宣告有些地方沒有。

查了下教材,做下小結,筆抄寫一遍,網上記一遍,備忘!

 

有以下三種情況不用函式宣告,可以直接調用:

1.被調函數傳回型別為整形或者char型,系統會自理。

舉例:

//代碼1#include <stdio.h>int main(){int a=30;double d=4.4334;printf("a=%d",call(a));return 0;}int call(int d){printf("HelloWorld\n");return d;}

 正確。如果將call函數的傳回型別改為double呢?

//代碼2#include <stdio.h>int main(){int a=30;double d=4.4334;printf("d=%f",call(d));return 0;}double call(double d){printf("HelloWorld\n");return d;}

 gcc表示對call函數的類型很迷惑很糾結~ 

D:\prj\core_c>gcc demo.c -o demo.outdemo.c:8:8: error: conflicting types for 'call'demo.c:5:16: note: previous implicit declaration of 'call' was here

2.被調函數定義在主函數之前。

上面的代碼2的call方法在在main之前,就不會有問題了。

3.在定義所有函數之前,先對調用函數做了聲明。

//代碼4#include <stdio.h>double call(double d);int main(){int a=30;double d=4.4334;printf("d=%f",call(d));return 0;}double call(double d){printf("HelloWorld\n");return d;}

 相當於做了外部聲明。

 

<2>形參。

舉例:

(1)函數的聲明,可以省去變數名。

int getMax(int a,int b);//可以省去變數名,方便編譯系統差錯:int getMax(int,int);

(2).宏定義時候的形參不需要類型,不用給它分配記憶體。 

#define P(d) printf("%d",d)

初學者筆記。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.