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

Time:10-26

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");

CodePudding user response:

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.

CodePudding user response:

You are trying to print the getcwd() function inside of the printf which is why it is throwing so many arguments error. If you want to get the current working directory using a getcwd(), then you can try using this code:

#include <unistd.h>
#include <stdio.h>
int main() {
    char cwd[256];
    if (getcwd(cwd, sizeof(cwd)) == NULL){
        perror("getcwd() error");
    }
    else{
        printf("current working directory is: %s\n", cwd);
    }
    return 0;   
}

Kindly provide more code so that I can try reproducing your error and fix your code.

  • Related