1

I'm currently working myself through an existing project and came to the following snippet:

int main(int argc, char** argv);

int main(int argc, char** argv)
{
    ...
}

and thats basically the whole file. Now, to my understanding, the first function declaration doesn't make sense at all: There is no other function between the provided lines that tries to call main.

Now my question is: Is there any case that I'm not aware of that would benefit from the first declaration (maybe with interdependencies to other files)? Or could it be safely deleted? Because of the size of the project, I'm currently not really able to test it, and I'm curious.

J.Fox
  • 13
  • 3
  • Do you have version control history? Maybe it made difference in the past. Anyways it's looks strange, and might be like poetry. Only the author know what this is really about. Looks like some weird sticking to some policy. – luk32 Oct 16 '17 at 09:57
  • 1
    maybe this is related? https://stackoverflow.com/questions/5020362/declare-main-prototype – Hafnernuss Oct 16 '17 at 10:02
  • 1
    *"There is no other function between the provided lines that tries to call main."* => And there better not be. [You may not call main from your own code.](https://stackoverflow.com/a/2532922/211160) – HostileFork says dont trust SE Oct 16 '17 at 10:03
  • @Hafnernuss - Not only is it related, if not for the language tag, I'd consider it a duplicate. In fact, [this answer](https://stackoverflow.com/a/5020400/817643) covers this very post. – StoryTeller - Unslander Monica Oct 16 '17 at 10:14
  • @luk32: Could be. Maybe the author just liked it this way. – J.Fox Oct 16 '17 at 10:26
  • @Hafnernuss: yeah, that was my reasoning. Just thought that there could be a special case. So, looks like a duplicate – J.Fox Oct 16 '17 at 10:27
  • @J.Fox Yeah well, I guess the definitive answer is that, there is no point in prototyping `main`, because if it was needed for something, that something would be almost certainly wrong. – luk32 Oct 16 '17 at 11:38

1 Answers1

1

The first line is what you call a prototype/signature, which represents the signature of the function: name, parameters, return type. I think here is all that would answer to your question but I'll try to say something shorter about it. The second one is the definition of the function, which is where you write the code for the function.

The prototype is telling "hey here's a function, it's made this way, takes these parameters and I'm going to use it in my code. You'll find its implementation somewhere in the code, but consider it existing and working".

The definition is telling "hey my function works this way".

Usually you would find useful to write the prototypes somewhere, and the relative definitions in a different file, for the sake of clean code.

The prototype is not mandatory, it's just a way to define and use a function before implementing it (said really poorly), but without a prototype you'll have to define it directly, and that can be really bad for readability, maintainability and understandability of your code.

That said, I think that is more than safe to delete that first line, since main() is defined immediately down and it's the entry point for your program. It's not called explicitly in the code because it's what is called when you launch your software.

magicleon94
  • 4,887
  • 2
  • 24
  • 53
  • 1
    Thanks. I was aware of the basic concepts of prototypes and implementations (even though your link helped a lot, especially with the terminology), but my question was more related to the special case of the usage of a main-prototype. So if there aren't any other fancy usages of prototypes, I guess my intuition was right. – J.Fox Oct 16 '17 at 10:22