I made a short code like below.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
int32_t main(int32_t argc, int8_t *argv[])
{
int32_t i;
if (argc < 1)
{
printf("Error\n");
}
for (i = 0 ; i < argc ; i++)
{
printf("argv[%d] = %s\n", i, argv[i]);
}
return 0;
}
And compiled like below, then I see a warning like below.
$ gcc -W -Wall main.c
main.c:6:9: warning: second argument of ‘main’ should be ‘char **’ [-Wmain]
int32_t main(int32_t argc, int8_t *argv[])
What is the best practice to use int8_t?
At its root I believe you're asking a style question, which means you're unlikely to get a definitive answer. Opinions on style are, well, opinions.
Some people believe that you should use C's "natural" types —
char,short,int,long, and theirunsignedvariants — most of the time, and that you should use exact-size types likeint32_tonly when you absolutely have to.Some people believe that the variability implied by the "natural" types is an unrelenting source of bugs, and they believe that you should use exact-size types always.
Now, with that said, the specific case of writing
is objectively wrong, for at least three reasons:
intis 16 bits, this wrongly declaresmain's return type, and the type of theargcargument, as a 32-bit type.charis unsigned, this wrongly declaresargvas an array of signed character pointers. That's probably what gcc was complaining about for you.mainis not a function whose function signature you get to pick. Somebody else declaredmain, somebody else is callingmain, your job is only to provide a definition formain. So you simply have to use the types somebody else specified, even if your rule is that you want to use exact-size types whenever you can. Here, you can't.Bottom line: Please use one of these two (equivalent) forms for
mainwith arguments:Unless you're writing "freestanding" code, anything else is confusing, misleading, nonstandard, or wrong. (It's also acceptable to define a
mainthat takes no arguments.)I would say, when you really, really need a tiny, 8-bit, signed integer, or perhaps when you're manipulating memory as signed bytes. But I would not go using
int8_tinstead ofchareverywhere, because it's going to cause you lots of problems, and it's not going to buy you anything.