What the call for the printf function should look like when displaying a value stored in a variable of type size_t? Thanks.
What the call for the printf function should look like when displaying a value stored in a variable of type size_t? Thanks.
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..
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.
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.
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.*
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; }
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.Generally the size_t variable should be cast to an unsigned long
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.
Because all the world is not Win32.
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.*
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.
Originally Posted by the pooperget it?Originally Posted by Dave's Link
> 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.
Originally Posted by the pooperIf 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.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.
[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.*
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.Contrary to the heresies espoused by some of the dwellers on the Western Shore, `int' and `long' are not the same type.
get it?
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.Well in the very recent past, unsigned int was 16 bits and size_t was 32 bits (on some 16 bit compilers)
I assumed neither.``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''
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.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
Yes.Originally Posted by the pooper
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.*
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)