Thread: size_t question

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    29

    size_t question

    What the call for the printf function should look like when displaying a value stored in a variable of type size_t? Thanks.

  2. #2
    Registered User
    Join Date
    Sep 2005
    Posts
    4

    size_t question

    hey csisz3r,

    "What the call for the printf function should look like when displaying a value stored in a variable of type size_t?"

    a variable cannot be of type size_t.... it must be either int, char, float, or even char*..if you understand what i mean. "size_t" is not a type...


    printf("%s", k); // where k is of type string,
    printf("%d", k); //where k is of type int
    printf("%f %c", a, b);//where a is of type float and b is of type char.

    Maybe if you can be clearer or frame your question better ..
    Take care..

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    size_t is a type - defined in stddef.h, but I don't believe printf is capable of displaying that type. It's probably a struct with an integer member that could be used - but you're going to need a pretty in depth reference to figure this one out - I personally don't have a copy of the standard.

  4. #4
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    I do have a copy of the standard. size_t is an implementation defined type, but it is some form of unsigned integer, capable of holding at least values upto 65535.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Generally the size_t variable should be cast to an unsigned long and thus the %lu specifier used in the call to printf. C99 added the %z modifier.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    If your compiler is c99 compliant, you can use the %zu specifier:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    
            int foo;
    
            printf("The size of foo is %zu\n", sizeof(foo));
    
            return 0;
    }
    ~/

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    42
    Generally the size_t variable should be cast to an unsigned long
    Why a long? Seems to me just a regular unsigned int is the correct data size, since 32-bit computers have 2^32 addressable memory locations and 64-bit computers have 2^64 addressable memory locations. And, consistent with that, if I do sizeof(size_t) on my computer here I get 4.

    This is supported by the fact that the sizeof operator is defined to result in an integer constant, and that size_t is defined as the type of the result of the sizeof operator.
    Last edited by the pooper; 09-17-2005 at 07:54 AM.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    42
    Nothing in that article contradicts what I said, nor is any part of my post specific to Win32 (barring the results of the example I ran on my personal computer). The terms 32-bit and 64-bit reflect hardware architecture, that is, the size of registers in a CPU and the number of addressable memory locations.

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by the pooper
    Nothing in that article contradicts what I said, nor is any part of my post specific to Win32 (barring the results of the example I ran on my personal computer). The terms 32-bit and 64-bit reflect hardware architecture, that is, the size of registers in a CPU and the number of addressable memory locations.
    Quote Originally Posted by Dave's Link
    Contrary to the heresies espoused by some of the dwellers on the Western Shore, `int' and `long' are not the same type.
    get it?

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    > Seems to me just a regular unsigned int is the correct data size
    Well in the very recent past, unsigned int was 16 bits and size_t was 32 bits (on some 16 bit compilers).
    Prior to C99, your best bet is the longest unsigned type you can have.
    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.

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by the pooper
    Nothing in that article contradicts what I said, nor is any part of my post specific to Win32 (barring the results of the example I ran on my personal computer). The terms 32-bit and 64-bit reflect hardware architecture, that is, the size of registers in a CPU and the number of addressable memory locations.
    10 Thou shalt foreswear, renounce, and abjure the vile heresy which claimeth that ``All the world's a VAX'', and have no commerce with the benighted heathens who cling to this barbarous belief, that the days of thy program may be long even though the days of thy current machine be short.

    This particular heresy bids fair to be replaced by ``All the world's a Sun'' or ``All the world's a 386'' (this latter being a particularly revolting invention of Satan), but the words apply to all such without limitation. Beware, in particular, of the subtle and terrible ``All the world's a 32-bit machine'', which is almost true today but shall cease to be so before thy resume grows too much longer.
    If you've ever had the displeasure of attempting to port supposedly portable code to a smaller architecture, like say trying to put internet protocols into an 8-bit device, you might get it. Assuming that things only grow larger means your code is only portable in one direction. But even today, or perhaps expecially today, it is important to know some of these things to keep the code you write usable as is in the future.

    [edit]A related DR or two.
    Last edited by Dave_Sinkula; 09-17-2005 at 10:42 AM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    42
    Contrary to the heresies espoused by some of the dwellers on the Western Shore, `int' and `long' are not the same type.


    get it?
    I was making a distinction between the two types, which is pretty much the opposite of believing them to be the same. That quotes does not apply at all to what I said.

    Well in the very recent past, unsigned int was 16 bits and size_t was 32 bits (on some 16 bit compilers)
    So ultimately you cannot count on size_t being any particular size, whether int, long int or even long long int. So the only way you could portably print the value of type size_t (prior to C99) would be to conditionally test the size of the type and execute a printf statement accordingly.

    ``All the world's a 386'' (this latter being a particularly revolting invention of Satan), ... Beware, in particular, of the subtle and terrible ``All the world's a 32-bit machine''
    I assumed neither.

    If you've ever had the displeasure of attempting to port supposedly portable code to a smaller architecture, like say trying to put internet protocols into an 8-bit device
    It was my understanding that, like the 32 and 64 bit architectures, a 16-bit architecture could only have 2^16 addressable memory locations. Likewise for an 8-bit architecture. Am I mistaken? If not than the native integer size should always have the necessary range of values to represent each location.

  14. #14
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by the pooper
    Am I mistaken?
    Yes.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  15. #15
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    IA-32 is a 32 bit architecture that can address more than 2^32 memory locations, 64 bit integer registers (mmx, mmX), and 128 bit registers (sse, xmmX)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM