当前位置:网站首页>Extern keyword function

Extern keyword function

2022-07-05 07:51:00 Yang_ Winston

grammar

extern Before variables and function declarations , Indicates that the variable or function has been defined in other files , Prompt the compiler to look for other files when compiling . besides ,extern It can also be used to specify links .

effect

  1. Declare external variables
    When declaring global variables , Different files are opaque when compiled by the compiler , For example A.c In the definition of int i, At the same time B.c In the definition of int i, The compiler will not report errors when compiling , But when links linking It will report an error and repeat the definition . When you need to use the same global variable , Such as : stay A.c It defines int i, stay B.c You need to call i, Only need B.c In a statement extern int i, Indicates that the variable has been defined in other files , There will be no errors when compiling , stay linking… It will automatically find the defined variables i.
  2. extern Function declaration
    extern void fun() It implies that this function may have been defined in other files , It and are defined as void fun(), No difference , It is useful for complex projects by adding extern Declaration to substitute include" *.h" To declare functions .
  3. Unilaterally modify the function prototype
    When declared extern void fun(int i,int j,int k) when , In the subsequent calls, if you press yes fun(x,y,z) There is no problem when calling the prototype of , But if you want to modify the function, such as input parameters , When called fun(x,y) At this point, the compiler will report an error , The solution is to get rid of extern, Declared in the header file void fun(int i,int j), And modify the function , Then include the header file of the function in the called file " *.h" that will do .
原网站

版权声明
本文为[Yang_ Winston]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140549119481.html