Thread: Not sure why %u is getting an error

  1. #1
    Registered User
    Join Date
    Aug 2012
    Location
    Florida
    Posts
    18

    Not sure why %u is getting an error

    I'm using gcc 7.5 with linux mint 19.1 64bit. The code is from Pointers in C, 3rd edition by Y Kanethar. Nube needs help. Thanks.


    Code:
    #include<stdio.h>
    int main()
    {
        int i = 3;
    
    
        printf("\nAddress of i = %u", &i);
        printf("\nValue of i = %u\n", i);
    
    
        return 0;
    
    
    }
    
    
    /*
    output:
    
    
    $ gcc -o prog1 prog1.c
    prog1.c: In function ‘main’:
    prog1.c:8:29: warning: format ‘%lu’ expects argument of type ‘long unsigned int’, but argument 2 has type ‘int *’ [-Wformat=]
      printf("\nAddress of i = %lu", &i);
                               ~~^   ~~
                               %ls

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    Use %p if you want to print a pointer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,127
    Code:
    printf("\nAddress of i = %p", (void *) &i);
    %p should be used and the address must be cast to a void *

  4. #4
    Registered User
    Join Date
    Aug 2012
    Location
    Florida
    Posts
    18
    Thank you Salem. Just starting out so I have a feeling I'll be back here.

  5. #5
    Registered User
    Join Date
    Aug 2012
    Location
    Florida
    Posts
    18
    Thanks rstanley ! Not sure why you added (void *) as program seems to work without it. Got a lot to learn here.

  6. #6
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,127
    Quote Originally Posted by mack99 View Post
    Thanks rstanley ! Not sure why you added (void *) as program seems to work without it. Got a lot to learn here.
    It will at least on some compilers especially when you turn up the warning level on the compiler. And you should compile with high warning levels!

  7. #7
    Registered User
    Join Date
    Aug 2012
    Location
    Florida
    Posts
    18
    Thanks again !

  8. #8
    Registered User
    Join Date
    Apr 2021
    Posts
    143
    Quote Originally Posted by mack99 View Post
    Thanks rstanley ! Not sure why you added (void *) as program seems to work without it. Got a lot to learn here.
    According to the standard, void * pointers are "wide" enough (that is, they are made up of enough bits) to store any kind of pointer used by the system.

    There are, or have been in the past, systems where different kinds of pointers require different amounts of storage -- that is, different #s of bits.

    For example, on some systems, a pointer to a function might be larger or smaller than a pointer to a data object.

    For example (2), there were systems where "byte" pointers (char * and void *) required more bits than "word" pointers.

    So it's possible that sizeof (char *) != sizeof (int *). It's also possible that sizeof (char *) != sizeof (void (*)(void)). (a function pointer)

    (See here for a discussion of specific examples.)

    The printf function has at least two format codes that involve pointers: p and n. The documentation for "%p" requires that the corresponding argument be a pointer to void. However, "%n" requires that its corresponding argument be a pointer to int.

    There is no explicit requirement (that I can find) that any "default argument promotions" apply to pointers. So I believe that how pointers are passed (to a varargs function) is ultimately implementation defined, but it will almost surely depend on the ABI in effect, since printf is a library function.

    An example of this is the handling of "far" pointers on MSDOS/PCDOS systems. The Borland Turbo-C library defined the format string "%p" to expect a far pointer. That is, the expectation was that the pointer would be extended to maximum size. On the other hand, the Microsoft C library defined the format string "%Fp" to print a far pointer. If the F modifier was not present, the value was expected to be a near (16-bit) pointer. (Both examples are quite old, hence "16 bit pointer". ;-)

    What does all this mean for your code? Casting a pointer to void * is guaranteed to work. And it is, in fact, possible for a type other than void * or char * to fail to work if not cast. But it's extremely unlikely, unless you're backporting code to older systems, or working in the embedded space.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-20-2016, 10:31 AM
  2. Compile error? Logic error? Syntax Error? Please help
    By Khody Afkhami in forum C Programming
    Replies: 4
    Last Post: 10-11-2014, 01:36 AM
  3. Replies: 4
    Last Post: 07-24-2011, 09:38 PM
  4. Replies: 1
    Last Post: 11-15-2010, 11:14 AM
  5. Replies: 3
    Last Post: 10-02-2007, 09:12 PM

Tags for this Thread