Thread: signed size_t/unsigned ptrdiff_t?

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129

    signed size_t/unsigned ptrdiff_t?

    Quote Originally Posted by C99 draft, fprintf
    z Specifies that a following d, i, o, u, x, or X conversion specifier applies to a size_t or the corresponding signed integer type argument; or that a following n conversion specifier applies to a pointer to a signed integer type corresponding to size_t argument.

    t Specifies that a following d, i, o, u, x, or X conversion specifier applies to a ptrdiff_t or the corresponding unsigned integer type argument; or that a following n conversion specifier applies to a pointer to a ptrdiff_t argument.
    What does "corresponding signed/unsigned type" mean? I tried to make a signed size_t and an unsigned ptrdiff_t type, but GCC wouldn't have it.
    Code:
    $ cat file.c
    #include <stddef.h> 
    
    int main ()
    {
            signed size_t x;
            unsigned ptrdiff_t y;
    
            return 0;
    }
    $ gcc -std=c99 -pedantic file.c
    file.c: In function 'main':
    file.c:5: warning: ISO C forbids nested functions
    file.c:5: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'x'
    file.c:5: error: 'x' undeclared (first use in this function)
    file.c:5: error: (Each undeclared identifier is reported only once
    file.c:5: error: for each function it appears in.)
    file.c:6: warning: ISO C forbids nested functions
    file.c:6: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'y'
    file.c:6: error: 'y' undeclared (first use in this function)
    $
    Last edited by robwhit; 09-26-2008 at 08:51 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    size_t is defined as an unsigned type, and ptrdiff_t is defined as a signed type, period. They are almost certainly typedef's of existing types, so the corresponding signed/unsigned type would be ... whatever the corresponding type is (long? long long?) But if you do %zd with a size_t variable, it will be printed as signed, since that's what d does, and if you do %tx with a ptrdiff_t variable, it will be printed as unsigned, since that's what x does.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    I think what they mean by "... or the corresponding signed integer type argument..." is that the format string may have a number of such type specifiers, each applying (AKA corresponding) to a respective argument.

    fprintf(fp, "&#37;d %u", abc, def)

    Two conversion specifiers, each corresponds to their respective arguments. The first corresponds to 'abc', the second to 'def' and so on.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Thanks all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. signed char type
    By sarahr202 in forum C++ Programming
    Replies: 9
    Last Post: 05-21-2009, 07:12 PM
  2. How to tell a type is signed or unsigned?
    By meili100 in forum C++ Programming
    Replies: 22
    Last Post: 05-08-2008, 08:21 PM
  3. Signed Char Overflow
    By coder8137 in forum C Programming
    Replies: 5
    Last Post: 11-17-2006, 08:25 AM
  4. - signed int to binary?
    By KnowledgeHungry in forum C++ Programming
    Replies: 7
    Last Post: 08-25-2003, 11:57 AM
  5. signed char storage
    By Leo_Vinay in forum C Programming
    Replies: 1
    Last Post: 01-19-2003, 06:57 AM