skip to Main Content

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

2

Answers


  1. 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: %sn", 
            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.

    Login or Signup to reply.
  2. You are trying to print the getcwd() function inside of the printf() function. That is why it is issuing a too 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: %sn", cwd);
        }
        return 0;   
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search