Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why main is not declared as extern

Tags:

c

When we want to use a function say void foo(void) in File1.c in my Main.c [ where my main function is ] - why do i not need to write:

extern int main(void);

In File1.c?

Assuming File1.c and Main.c are in a Single TU.

like image 994
Expert Novice Avatar asked Dec 09 '22 05:12

Expert Novice


2 Answers

You never need to write extern with function declarations. Functions have external linkage by default. There's a strange unexplainable habit observed in some older code: to add extern to all external function declarations. (This is probably what led to your question about main.) In reality, it is completely unnecessary, serves no purpose and only clutters the code.

You can surely declare main with extern, if you so desire. But it is totally redundant.

like image 54
AnT Avatar answered Dec 25 '22 00:12

AnT


For a start, functions are extern by default.

Furthermore, main is "special"; the linker treats it differently.

like image 45
Oliver Charlesworth Avatar answered Dec 25 '22 01:12

Oliver Charlesworth