C語言 extern3 全域變數的使用

來源:互聯網
上載者:User

標籤:

和函數的全域使用極其類似:

第一種方法,也是最簡單的:

在 first.h 中定義,

int G_Flag=78;

在對應的first.c中使用:

#include "first.h"#include <stdio.h>void main(){    printf("%d",G_Flag);}

 

第二種方法(複雜的):

在second.c中定義,在對應的second.h中不使用extern聲明,在first.h中使用extern聲明,在first.c中使用

second.c:

//多檔案測試#include "first.h"#include <stdio.h>/*
*其他定義子函數
*/
//在.c定義
int G_Flag=25;

second.h中聲明(不使用extern)

int G_Flag;

first.h中聲明(使用extern)

 extern int G_Flag;

first.c中調用:

#include "first.h"#include <stdio.h>void main(){    printf("%d",G_Flag);}

第二種方法(簡單的):

在second.h中直接定義,在first.h中使用extern聲明,在first.c中使用

second.h中直接定義:

 int G_Flag=1224;

first.h中聲明(使用extern)

然後在first.c中調用即可,和上邊的first.c相同

 extern int G_Flag;

C語言 extern3 全域變數的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.