I have heard that c99 has the format specifier %zu for this and in C 89 % lu was used. What about scanning size_t variables ?
eg.
Is this okay or a cast is required here as well ?Code:
size_t a;
scanf("%lu", &a);
Printable View
I have heard that c99 has the format specifier %zu for this and in C 89 % lu was used. What about scanning size_t variables ?
eg.
Is this okay or a cast is required here as well ?Code:
size_t a;
scanf("%lu", &a);
>Is this okay or a cast is required here as well ?
I don't think you can read directly into a size_t variable in C89. I think you would need to first read into some other type variable, for example an unsigned long, long, or long long, then assign this to a size_t.
It is possible to work out what a size_t actually is. Methods include;
1) Compare sizeof(size_t) with sizeof(unsigned short), sizeof(unsigned long), or [C99 only] sizeof(unsigned long long). Whichever one matches is the actual type of a size_t for your implementation.
2) [C99 only] Look at SIZE_MAX (from <stdint.h>) and compare with USHRT_MAX, ULONG_MAX, and ULLONG_MAX (the maximum values representable respectively in unsigned short, unsigned long, and unsigned long long respectively, from <limits.h>.
If there is no match with these methods, then that would mean size_t is a different type from any unsigned integral type supported by your compiler. In practice, I've yet to encounter any compiler that will not yield a match but, strictly speaking, it could occur.
Note: C89 and C++ do not support long long integral types (although some compilers do).