warning: format ‘%x’ expects argument of type ‘unsigned int’

23,928

Solution 1

You are getting the warnings because of the following statements

    printf("Name buffer address:    %x\n", buffer);
    printf("Command buffer address: %x\n", c);

%x expects an unsigned int, whereas you're supplying a pointer.

To refer, C11 standard, chapter §7.21.6.1

o,u,x,X
The unsigned int argument is converted to unsigned octal (o), unsigned decimal (u), or unsigned hexadecimal notation (x or X) in the style dddd; [...]

Supplying invalid argument invokes undefined behavior.

You should be using %p to print the address

p The argument shall be a pointer to void.[...]

and cast the argument to void *, because for pointer types no default argument promotion takes place.

Having said that,

  1. main() should be int main(void), at least, to conform to the standard.
  2. You need to forward declare your function bo() because implicit declarations are bad and non-standard now.

Solution 2

To print an address use "%p" instead of "%x". You also need to cast to void *

printf("Name buffer address:    %p\n", (void *) buffer);
Share:
23,928
Ankh2054
Author by

Ankh2054

Charles Holtzkampf leads the technology efforts at EOS42 and is designing our infrastructure that will support the EOS.IO economy. He has spent the past 20 years honing his skills in all matters of IT, from designing high performance VDI clusters to maintaining a 4000 node networking cluster for a leading business centre provider. He has spend time working as a security penetration tester and dabbles in Python development. In his lifetime he started a starfish type organisation called BOMelakiesie and for a time hosted the Burning man Afrika's website. He has extensive experience with programming, performance monitoring, increasing efficiency and web services. He participated in the launch of the EOS Dawn 3.0 community test net and is excited to see the world change for the better.

Updated on July 18, 2022

Comments

  • Ankh2054
    Ankh2054 almost 2 years

    When I try and compile this I get the following error, not sure why...

    warning: format ‘%x’ expects argument of type ‘unsigned int’, but argument 2 has type ‘char *’ [-Wformat=]

    printf("Name buffer address:    %x\n", buffer);
    

    The code:

    #include <string.h>
    #include <stdio.h>
    
    main(){
            char name[200];
            printf("What is your name?\n");
            scanf("%s", name);
            bo(name, "uname -a");
    }
    
    int bo(char *name, char *cmd){
            char c[40];
            char buffer[40];
            printf("Name buffer address:    %x\n", buffer);
            printf("Command buffer address: %x\n", c);
            strcpy(c, cmd);
            strcpy(buffer, name);
            printf("Goodbye, %s!\n", buffer);
            printf("Executing command: %s\n", c);
            fflush(stdout);
            system(c);
    }