C with the release of printf ("% d", astatbuff-> st_size);
int lsdetails(struct stat *astatbuff) { printf("%d", astatbuff->st_size); printf("%d", astatbuff->st_atime); printf("%s\n", getpwuid(astatbuff->st_uid)->pw_name); return 0; } warning: format '% d expects an argument of type' int, but argument 2 is of type '__off_t [-Wformat]
I received the error message above, but I do not understand why. My impression is that I pass only one argument to both st_size and st_atime .
Unlike C, which starts counting from 0 (array elements :-), gcc starts counting the function arguments at 1. Thus, in printf (fmt, value) argument 1 will refer to fmt , and argument 2 means value . Easy, right?
As for the correct integer type for printf a __off_t , there is currently no 100% guaranteed and portable way. It is best to use it for the widest unsigned type that your implementation supports. Please note that an unsigned long can only be 32 bits wide and you will have problems with files> = 4 GB. If you have a C99 implementation or unsigned long long supported, you should be fine with
printf("%llu", (unsigned long long)astatbuff->st_size); There are discussions in the current POSIX standardization group to provide more printf () format specifiers that match other POSIX types, such as off_t , pid_t , etc. As soon as it comes out the door (don't hold your breath), printing file sizes will be a little more portable and elegant.
I get an abover error message, but I do not understand why. My impression is that I pass only one argument to both
st_sizeandst_atime.
But you pass two arguments to printf ,
- format string
- member of structure
The second argument is of type __off_t , but the format for the arguments is int . What is the correct format for __off_t , I donβt know if %ld or %zd have a good chance of being correct, but to play safely, discard on intmax_t and use %jd .