Suppose I wish to write a C program (C99 or C2011) that I want to be completely portable and not tied to a particular architecture.

It seems that I would then want to make a clean break from the old integer types (int, long, short) and friends and use onlyint8_t, uint8_t, int32_t and so on (perhaps using the the least and fast versions as well).

What then is the return type of main? Or must we strick with int? Is it required by the standard to be int?

This is because of a typedef, no doubt. It seems this is one case where we are stuck with having to use the unspecified size types, since it's not truly portable unless we leave the return type up to the target architecture. Is this interpretation correct? It seems odd to have "just one" plain old int in the code base but I am happy to be pragmatic.

What portability issue do you perceive from using int as the return type from main()?
–
JasonDFeb 12 '13 at 14:52

int is portable in the sense it will compile on any machine. Now you will get problems if you say try to return 12344567891233445567 but let's be honest when was the last time you did that? All the numbers you return should be reasonably small. e.g. the error code.
–
David Mokon BondFeb 12 '13 at 14:53

I realize that using int makes the program portable in just this case, whereas everywhere else it seems portability demands the use of the fixed size types. Maybe it is just a necessary asymmetry.
–
Ray ToalFeb 12 '13 at 14:54

2

For portability you must assume a level of compliance with the standards. If an implementation is non-compliant, all bets are off. Otherwise, you can use ints in many situations without worries about portability. For example, you can assume a range of +/- 32767.
–
JasonDFeb 12 '13 at 15:05

Does your code's portability REALLY depend on the size of int that much anyway? For a large amount of usage, [such as short loops], the size of the integer is completely irrelevant. You are just putting more complexity into the code and potentially making the code slower by forcing the compiler to use types that isn't necessarily the ideal one. That may indeed help in a few places, but in most places, it's probably not that important.
–
Mats PeterssonFeb 12 '13 at 15:28

2 Answers
2

Suppose I wish to write a C program (C99 or C2011) that I want to be
completely portable and not tied to a particular architecture.

It seems that I would then want to make a clean break from the old
integer types (int, long, short) and friends and use only int8_t,
uint8_t, int32_t and so on (perhaps using the the least and fast
versions as well).

These two affirmations, in bold, are contradictory. That's because whether uint32_t, uint8_t and al are available or not is actually implementation-defined (C11, 7.20.1.1/3: Exact-width integer types).

If you want your program to be truly portable, you must use the built-in types (int, long, etc.) and stick to the minimum ranges defined in the C standard (i.e.: C11, 5.2.4.2.1: Sizes of integer types),

Per example, the standard says that both short and int should range from at least -32767 to at least 32767. So if you want to store a bigger or lesser value, say 42000, you'd use a long instead.