warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ using argv

3.2k views Asked by At

I don't even know what is happening, I just started a new project and setup a basic cat just to make sure everything was working, and this happened.

#include "stdlib.h"
#include "stdio.h"

int main(int argc, char *argv) {
    printf("%s",argv[0]);
    return 0;
}

That's it, I reinstalled gcc, g++, and both multilibs. I really have no clue what to even think.

2

There are 2 answers

1
Unsigned On

The declared type of argv is wrong. The signature of main should be one of:

int main(int argc, char **argv);
int main(int argc, char *argv[]); // Functionally equivalent to above

Note that main can also take void to ignore parameters, but this is not what you're looking for here.

I personally prefer the second form listed above as I find it more intuitive (array of char*) vs the first (pointer to char*), but since arrays are merely pointer arithmetic in C, either will do.

1
BufferOverflow On

The second argument to the main function is normally defined as char **argv or as char *argv[]; both ways are correct.

And the include sentences are also wrong. With #include "file.h" it will look after file.h in the current folder, but if your are including a header file from C standard library, it should be #include <file.h>.