C语言 extern

ChatGPT 3.5 国内中文镜像站免费使用啦

零基础 C/C++ 学习路线推荐 : C/C++ 学习目录 >> C 语言基础入门


一.简介

如果一个变量使用用关键字 extern ,对该变量作“外部变量声明”,表示该变量是一个已经定义的外部变量。有了此声明,就可以从“声明”处起,合法地使用该外部变量。

/******************************************************************************************/
//@Author:猿说编程
//@Blog(个人博客地址): www.codersrc.com
//@File:C语言教程 - C语言 extern
//@Time:2021/07/18 07:30
//@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
/******************************************************************************************/ 


/* file1.c */

int x = 10; 
int func1();



/* file2.c */

extern int x ;     //该变量是已经在外部定义,此处只做一个声明
extern int func1();//该函数是已经在外部定义,此处只做一个声明

int main(void)
{
    printf("x = %d",x); //  x = 10
    fun1();

    return 0;
}

二.extern修饰变量


1.在单文件中使用extern

首先我们回到全局变量中有讲到:在所有函数外部定义的变量称为全局变量(Global Variable)它的作用域默认是从定义变量的位置到本源文件结束都有效。

/******************************************************************************************/
//@Author:猿说编程
//@Blog(个人博客地址): www.codersrc.com
//@File:C语言教程 - C语言 extern
//@Time:2021/07/18 07:30
//@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
/******************************************************************************************/ 


#include <stdio.h>

void func1(){
    x += 10;
    y += 20;  
    printf("函数:%s  中 x = %d   y = %d \n",__FUNCTION__,x,y); 
    
}

int x = 10;
int y = 20;

void func2(){
    x += 10;
    y += 20;
    printf("函数:%s  中 x = %d   y = %d \n",__FUNCTION__,x,y);
    
}
int main(){
    
    func1();
    func2();
    printf("函数:%s  中 x = %d   y = %d \n",__FUNCTION__,x,y); 
	
    return 0;
}
/*
输出:

main.cpp: In function ‘void func1()’:
main.cpp:6:5: error: ‘x’ was not declared in this scope
    6 |     x += 10;
      |     ^
main.cpp:7:2: error: ‘y’ was not declared in this scope
    7 |  y += 20;
      |  ^

*/  

对于上面的编译器报错,我们可以通过 extern 来解决这个问题,示例代码如下:

/******************************************************************************************/
//@Author:猿说编程
//@Blog(个人博客地址): www.codersrc.com
//@File:C语言教程 - C语言 extern
//@Time:2021/07/18 07:30
//@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
/******************************************************************************************/ 


#include <stdio.h>

void func1(){
	
    extern int x; //声明一个外部变量
    extern int y; //声明一个外部变量

    x += 10;
    y += 20;  
    printf("函数:%s  中 x = %d   y = %d \n",__FUNCTION__,x,y); 
    
}

int x = 10;  //全局变量
int y = 20;

void func2(){
    x += 10;
    y += 20;
    printf("函数:%s  中 x = %d   y = %d \n",__FUNCTION__,x,y);
    
}
int main(){
    
    func1();
    func2();
    printf("函数:%s  中 x = %d   y = %d \n",__FUNCTION__,x,y); 
	
    return 0;
}
/*
输出:

函数:func1  中 x = 20   y = 40 
函数:func2  中 x = 30   y = 60 
函数:main   中 x = 30   y = 60 

*/

2.在多文件中使用extern

/******************************************************************************************/
//@Author:猿说编程
//@Blog(个人博客地址): www.codersrc.com
//@File:C语言教程 - C语言 extern
//@Time:2021/07/18 07:30
//@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
/******************************************************************************************/ 

/****max.c****/
#include <stdio.h>

/*定义两个全局变量*/
int x=10;
int y=20;
int max()
{
    return (x > y ? x : y);
}


/***main.c****/
#include <stdio.h>

/*extern 声明外部变量*/
extern int x ;
extern int y ;

int main(void)
{
    printf("x = %d  y = %d\n",x,y);
    return 0;
}

有了 extern 声明的外部变量可以直接使用,注意:使用 extern 声明的外部变量不需要再次初始化,仅仅只是作为一个声明而已,该变量的定义已经在其他文件中完成!


三.extern修饰函数

使用 extern 修饰函数,使用原理和上面 extern 修饰变量类似,有 extern 修饰之后,可以作为一个外部声明,我们直接调用即可;

/******************************************************************************************/
//@Author:猿说编程
//@Blog(个人博客地址): www.codersrc.com
//@File:C语言教程 - C语言 extern
//@Time:2021/07/18 07:30
//@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
/******************************************************************************************/ 


/****test.c****/
#include <stdio.h>

/*定义函数*/
int test_printf()
{
    printf("this func is %s\n",__FUNCTION__); 
    return 0;
}


/***main.c****/
#include <stdio.h>

/*extern 声明外部函数*/
extern int test_printf() ;

int main(void)
{
    test_printf();
    return 0;
}

四.猜你喜欢

  1. C语言 数组下标越界和内存溢出区别
  2. C语言 使用指针遍历数组
  3. C语言 指针和数组区别
  4. C语言 指针数组和数组指针区别
  5. C语言 野指针
  6. C语言 函数值传递和址传递
  7. C语言 函数不定长参数
  8. C语言 函数指针
  9. C语言 指针函数
  10. C语言 回调函数 callback
  11. C语言 #pragma once
  12. C语言 #include <> 与 #include “” 区别
  13. C语言 const 修饰函数参数
  14. C语言 const 和 define 区别
  15. C语言 #运算符
  16. C语言 ##运算符
  17. C语言 __VA_ARGS__
  18. C语言 ##__VA_ARGS__
  19. C语言 函数不定长参数 ##__VA_ARGS__经典案例
  20. C语言 va_start / va_end / va_arg 自定义 printf 函数
  21. C语言 main 函数
  22. C语言 main 函数参数 main(int argc, char *argv[])
  23. C语言 局部变量
  24. C语言 全局变量
  25. C语言 全局变量和局部变量区别
  26. C语言 static
  27. C语言 extern

ChatGPT 3.5 国内中文镜像站免费使用啦
© 版权声明
THE END
喜欢就支持一下吧
点赞2 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容