Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does int main() need a declaration on C++?

I was taught that functions need declarations to be called. To illustrate, the following example would give me an error as there is no declaration for the function sum:

#include <iostream>  int main() {   std::cout << "The result is " << sum(1, 2);   return 0; }  int sum(int x, int y) {   return x + y; }  // main.cpp:4:36: error: use of undeclared identifier 'sum' //  std::cout << "The result is " << sum(1, 2); //                                   ^ // 1 error generated. 

To fix this, I'd add the declaration:

#include <iostream>  int sum(int x, int y); // declaration  int main() {   std::cout << "The result is " << sum(1, 2);   return 0; }  int sum(int x, int y) {   return x + y; } 

Why the main function doesn't need the declaration, as other functions like sum need?

like image 634
Vinicius Brasil Avatar asked Apr 01 '19 14:04

Vinicius Brasil


People also ask

Does the main function have to be declared in C?

main is special in that there's typically no need for a separate declaration of it in your code; you don't normally call main explicitly.

Is int main () mandatory in C?

Not just int main() but one can skip writing main() entirely. main() is the entry point of a c program, this is a universal fact but actually when we dive deep, it becomes clear, that main() is called by another function called _start() which is actually the very first function called at the abstract level.

Is int main a variable declaration?

the difference is declaration of function and variables, that is in the function, int main() int is return type of the main function and in the variable, int s=23, int is data type of variable s. int main() means you want your output only in integer , but in int variable you're declaring data type of that variable .

What does int main () do in C?

int main represents that the function returns some integer even '0' at the end of the program execution. '0' represents the successful execution of a program. int main(void) represents that the function takes NO argument.


2 Answers

A definition of a function is also a declaration of a function.

The purpose of a declaring a function is to make it known to the compiler. Declaring a function without defining it allows a function to be used in places where it is inconvenient to define it. For example:

  • If a function is used in a source file (A) other than the one it is defined in (B), we need to declare it in A (usually via a header that A includes, such as B.h).
  • If two or more functions may call each other, then we cannot define all those functions before the others—one of them has to be first. So declarations can be provided first, with definitions coming afterward.
  • Many people prefer to put “higher level” routines earlier in a source file and subroutines later. Since those “higher level” routines call various subroutines, the subroutines must be declared earlier.

In C++, a user program never calls main, so it never needs a declaration before the definition. (Note that you could provide one if you wished. There is nothing special about a declaration of main in this regard.) In C, a program can call main. In that case, it does require that a declaration be visible before the call.

Note that main does need to be known to the code that calls it. This is special code in what is typically called the C++ runtime startup code. The linker includes that code for you automatically when you are linking a C++ program with the appropriate linker options. Whatever language that code is written in, it has whatever declaration of main it needs in order to call it properly.

like image 167
Eric Postpischil Avatar answered Sep 28 '22 16:09

Eric Postpischil


I was taught that functions need declarations to be called.

Indeed. A function must be declared before it can be called.

why we don't add a declaration for the main function?

Well, you didn't call main function. In fact, you must not call main at all1, so there is never a need to declare main before anything.

Technically though, all definitions are also declarations, so your definition of main also declares main.


Footnote 1: The C++ standard says it's undefined behaviour to call main from within the program.

This allows C++ implementations to put special run-once startup code at the top of main, if they aren't able to have it run earlier from hooks in the startup code that normally calls main. Some real implementations do in fact do this, e.g. calling a fast-math function that sets some FPU flags like denormals-are-zero.

On a hypothetical implementation, calling main could result in fun things like re-running constructors for all static variables, re-initializing the data structures used by new/delete to keep track of allocations, or other total breakage of your program. Or it might not cause any problem at all. Undefined behaviour doesn't mean it has to fail on every implementation.

like image 32
eerorika Avatar answered Sep 28 '22 15:09

eerorika