Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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);
}
like image 340
Ankh2054 Avatar asked Jan 10 '16 13:01

Ankh2054


People also ask

What is the type of argument in%X format?

Ask a Question Warning : format ‘%x’ expects argument of type ‘unsigned int’, but argument 2 has type ‘int *’ +2votes askedSep 9, 2018in Programming Languagesby praveen(18.0kpoints)

Why does%X take an unsigned int as an argument?

According to the C99 specification, %X takes an unsigned int argument, but you passed &v [i] which is an int*. Your compiler is warning you quite clearly of this mismatch. This mismatch may or may not be significant, it depends on the details of your compiler implementation.

What does%X mean in a warning warning?

Warning - format ‘%x’ expects argument of type ‘unsigned int’, but argument 2 has type ‘int *’ - DiscoverBits I found on a C tutorial website that they are using %x to print the address contained in a pointer.

What type of argument does%X take?

According to the C99 specification, %X takes an unsigned int argument, but you passed &v [i] which is an int*. Your compiler is warning you quite clearly of this mismatch.


2 Answers

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.
like image 112
Sourav Ghosh Avatar answered Oct 13 '22 14:10

Sourav Ghosh


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

printf("Name buffer address:    %p\n", (void *) buffer);
like image 34
Iharob Al Asimi Avatar answered Oct 13 '22 15:10

Iharob Al Asimi