0

I've seen some people use int main() for the main function while some use int32_t main(). What is the difference between both and where to use which.

  • 8
    I've never seen anyone use `int32_t main`. It is confusing, though on most platforms `int32_t` is just a typedef for `int` – UnholySheep May 28 '21 at 17:34
  • 1
    `int32_t main` is not a standard `main` signature, but on some systems `int32_t` might be just an alias for `int`. – Eugene Sh. May 28 '21 at 17:34
  • They're both wrong, so not much different. Use `int main(void)` – William Pursell May 28 '21 at 17:34
  • 4
    @WilliamPursell It depends on the language. In C++ `int main()` is correct. In C it's `int main(void)` – Thomas Sablik May 28 '21 at 17:37
  • The standards require implementations to support `int main()`, but do not require support of `int32_t main()`. You may get lucky (or unlucky, depending on how you look at it) and find that `int32_t` and `int` happen to be the same type with your implementation (compiler and library) - but the standards don't require that, so the code may not build correctly on different implementations. – Peter May 28 '21 at 17:39
  • 1
    @WilliamPursell - `int main()` is correct in both C and C++. `int main(void)` is a stylistic preference. – Peter May 28 '21 at 17:40
  • @ThomasSablik: `int main(void);` and `int main();` only have different meaning in C *if the semicolon is present*. With a `{` instead of a `;`, the meaning is identical. – Chris Dodd May 28 '21 at 17:46
  • 2
    @ThomasSablik C17 6.7.6.3/14 "An empty list in a function declarator that is part of a definition of that function specifies that the function has no parameters." – Ian Abbott May 28 '21 at 17:47
  • On platforms where `int` is not 32-bits, the `int32_t main()` will convert the return code (to the OS) to a 32-bit value. This may cause issues with the operating system when the operating system is not expecting 32-bits (such as 16-bits or 64-bits). – Thomas Matthews May 28 '21 at 17:54
  • Choose one of C or C++ and delete the other tag. If you want to ask about both, post separate questions. Do not tag both C and C++ except when asking about interactions or differences between the languages. – Eric Postpischil May 28 '21 at 18:03

2 Answers2

5

(This answer is for C.)

For a hosted C implementation (in contrast to a “freestanding” or “embedded” implementation, the 2018 standard says, in 5.1.2.2.1 1 that main shall be defined with a return type of int, except that an implementation may define other acceptable definitions. Specifically, it just says the return type shall be int, not a type that is just compatible with or the same width as int.

What is int32_t? It is specified by 7.20, which discusses <stdint.h>. 7.20 1 says this header declares various integer types. 7.20.1.1 says “The typedef name intN_t designates a signed integer type with width N, no padding bits, and a two’s complement representation…” It does not say these types are necessarily the same as the types char, short, int, long, and so on, even if their properties match. A “typedef name” is an alias for another type, not a type by itself, but the int32_t typedef name could be a name for some type other than int, even a type that is otherwise indistinguishable from int.

Thus:

  • Your C implementation could have two types int and int32_t that have the same properties except for the fact they are different types, where the latter is a typedef name for some built-in type. If so, these are different types, and int32_t main(void) does not satisfy the requirement of 5.1.2.2.1 1 unless your C implementation specifically documents that main may be defined this way.
  • Your C implementation could have int being a 16-bit type while int32_t is a typedef name for long. Again, int32_t main(void) would not satisfy 5.1.2.2.1 1.
  • Your C implementation could have int32_t being a typedef name for int. In this case, int32_t main(void) would satisfy 5.1.2.2.1 1.

There is really no point to using int32_t, though. int accomplishes all that is necessary.

Addendum

There is some discussion in the comments that int main() is wrong for C. C 2018 5.1.2.2.2.1 1 says that main may be defined in a way “equivalent” to the forms it shows, int main(void) and int main(int argc, char *argv[]). When a function is defined using () for the parameter list, the function is defined to have no parameters, and this is equivalent to using (void). (Note that using () in a declaration that is not a definition leaves it unspecified whether there are any parameters, but a definition settles it.)

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • 1
    There are even a couple of examples of `int main()` being used within the standard – 6.5.3.4/8 (EXAMPLE 3) and 6.7.6.3/20 (EXAMPLE 4). – Ian Abbott May 28 '21 at 17:56
0

The C Standard has a pretty clear cut answer to this.

5.1.2.2.1 Program startup

  1. The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:

    int main(void) { /* ... */ }  
    

    or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):

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

    or equivalent; or in some other implementation-defined manner

Using int32_t would be by definition "non-standard". In practice, almost all architectures are simply going to declare int32_t as typedef int int32_t, so it's unlikely to be a problem, but it's always a bad idea.

Jon Reeves
  • 2,426
  • 3
  • 14