Cersei
Cersei

Reputation: 95

In C, can a function called Main() be created?

Can a function called Main(), with a capital M, be created in C?
Since two elements with the same name but different cases are treated as separate entities, in my opinion Main() can be created but I just need to confirm.

Upvotes: 1

Views: 1388

Answers (5)

Allan Mayers
Allan Mayers

Reputation: 195

Only if you write one or more of its letters in uppercase, I think , Like Main or MAIN otherwise your compiler won't be able to tell which main you want because main is already a function in C. Note: I am a beginner too so I could well be wrong.

Upvotes: 0

Sourav Ghosh
Sourav Ghosh

Reputation: 134326

You have already got the answer, but just to add a bit of authoritative quotes, directly from C11, chapter §5.1.2

Execution environments:

Two execution environments are defined: freestanding and hosted. In both cases, program startup occurs when a designated C function is called by the execution environment. [...]

then, §5.1.2.2, Hosted environment, p1, "Program startup"

The function called at program startup is named main. [...]

That said, as we know, C is case-sensitive, (quoting §6.4.2.1/2, "Identifiers", General)

An identifier is a sequence of nondigit characters (including the underscore _, the lowercase and uppercase Latin letters, and other characters) and digits, which designates one or more entities as described in 6.2.1. Lowercase and uppercase letters are distinct. [....]

so an identifier without the same case as main() is not the same as main(), be it Main(), MAIN(), maiN() or any other combination possible. They all are different identifiers as far as C is concerned.

So, the answer to

In C, can a function called Main() be created?

is yes, certainly.

Just to clear up a bit more, if you only create Main() and leave out main(), compiler all by itself will have no way to know that you meant main() by writing Main(), since they are different identifiers to the compiler.

Upvotes: 1

David C. Rankin
David C. Rankin

Reputation: 84561

Sure, why not? The preprocessor and compiler do not care that "Main" and "main" are substantially similar. C is case-sensitive, therefore "Main" and "main" are two completely different names. (it may not help readability, but there is no technical reason you can't) E.g.,

#include <stdio.h>

int Main (int argc, char **argv)
{
    for (int i = 1; i < argc; i++)
        printf (" %2d : %s\n", i, argv[i]);

    return argc;
}

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

    printf ("\n %d arguments.\n", Main (argc, argv) - 1);

    return 0;
}

Example Use/Output

$ ./bin/mainMain The quick brown fox jumps over a lazy dog.
  1 : The
  2 : quick
  3 : brown
  4 : fox
  5 : jumps
  6 : over
  7 : a
  8 : lazy
  9 : dog.

 9 arguments.

Upvotes: 4

chris01
chris01

Reputation: 12321

int Main (void)
{
   return 0;
}

int main (void)
{
   return 0;
}

Compiles und links perfectly. It is case-sensitive.

Upvotes: 2

Malcolm McLean
Malcolm McLean

Reputation: 6404

It's terrible idea and some environments that aren't strictly conforming might reserve that name. But the linker is supposed to be case sensitive and Main with a capital is not reserved.

Upvotes: 1

Related Questions