Search code examples
cvisual-studio-codeprintffunction-call

C warning error: too many arguments for format [-Wformat-extra-args], with no format?


I am getting this problem in C programming. I'm new to C so I would like some help.

Anyine know why this is happening?

I'm trying to get the dir path using getcwd and I'm not even formatting so I don't get why I'm getting this warning? My code is below:

replace.c: In function ‘main’:
replace.c:49:8: warning: too many arguments for format [-Wformat-extra-args]
printf("Search begins in current folder: ", 
       getcwd(currDir,
       sizeof(currDir)), "\n");

Solution

  • The format string in the call of printf does not contain conversion specifiers. So the second and the third arguments apart from the format string are redundant.

    It seems you forgot to include the conversion specification %s in the format string.

    For example

    printf( "Search begins in current folder: %s\n", 
            getcwd( currDir, sizeof( currDir ) ); 
    

    Pay attention to that the call of getcwd can return a null pointer. So you need to check the return value before using the call of printf.