开发者

Exact meaning of "extern" in C and header file name equivalence to code file name

I have Googled and read, but didn't find answers for these three simple questions...

  1. Should header file name be equal to code file name? For example, I have function void foo() declared in foo.h. It is used in main.c which includes foo.h Must foo() be implemented in foo.c? What if it would be implemented in foox.c? Because I have a source code that has decoder.h header but decode.c code file and everything seems to work. There is no decoder.c nor decode.h files in the project.

  2. What is "extern" used for when functions are declared with "extern". In the said project, decoder.h declares extern functions while decode.c implements them. How does extern works here and how is it actually supposed to work? I always thought extern is used to let the compiler know it will find this somewhere else (like I declare a variable in main.c, which includes foo.h, in foo.c which implements functions from foo.h I want to change that variable's value, so I declare it as extern).

  3. Also, minor question on C syntax, I have a code that has function implementations that look like this

    int function(param1,param2,开发者_运维问答param3)
    int param1,*param2;
    char param3;
    {
        function body
    }
    

    My Qt Creator complains about this code and code navigation does not work, but the code compiles and executes well. What is this syntax? I have never seen it before...


  1. The C language does not care what you name your source and header files. You can use any names your compiler will accept, and put any function in any .c file you wish. Some other tools may care, but the language does not. Indeed, the language does not care if you name your source file bar.source instead of foo.c (but, again, your compiler may).

  2. extern tells the compiler that the variable is not defined in this compilation unit (the .c file plus all headers it includes), but somewhere else. You pretty much only need to use it when referring to a global variable defined in some other compilation unit. You can also use it with functions, but it is implicit, so not needed.

  3. The syntax you show is the really old syntax for defining functions. It was used before the first C standard, until the late 1980s. Don't use it anymore. The rules for how argument types are handled are archaic and unnecessarily complex, and using new-style function declarations and definitions make all the bad things go away.

Your example would be better written as:

int function(int param1, int *param2, char param3)
{
    function body
}

Only problem is that the old-style functions can't pass a char as an argument, so the last parameter should really be int param3 instead.


1) There are no requirements for the naming of headers. It is a common convention to use the same name for the .h and .c files.

2) Extern is used to specify linkage. It could be a variable that is declared in another header or source file. It could also be for declaring linkage in another language e.g. extern "c".

3) That syntax is original K&R c syntax. It is very old.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜