2

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...

3
  • Please use slightly modern C. And Qt Creator is C++. Commented Nov 2, 2010 at 14:22
  • For Q2, see SO 1433204 - one of probably a number of similar questions and answers. Commented Nov 2, 2010 at 14:34
  • Well, Qt Creator is C++, but i have to use code that is C code. When i have alot of free work time i will probably rewrite the whole code into C++ code but so far Qt Creator compiles C code for me as well as C++ code. The 3) syntax was the only thing that it couldnt process correctly Commented Nov 2, 2010 at 16:02

2 Answers 2

6
  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.

Sign up to request clarification or add additional context in comments.

Comments

-1

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.

1 Comment

Using extern to declare language linkage is a C++ feature. It does not exist in C.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.