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 argument of type ‘int’, but argument 2 has type ‘__off_t’ [-Wformat]
I received the above error message but I do not understand why. I am under the impression that I am only passing one argument for both st_size
and st_atime
.
What is the correct way to use printf to print a size_t in C/C++? We should use “%zu” to print the variables of size_t length. We can use “%d” also to print size_t variables, it will not show any error.
This post is about the formatted printing in C. The printf function of C can do a lot more than just printing the values of variables. We can also format our printing with the printf function. We will first see some of the format specifiers and special characters and then start the examples of formatted printing.
But, in one of your printf statements your are using the %d flag but intstead need to use the %ld. Show activity on this post. Your code doesn't show up properly. The error that you are getting is just a warning. It means that you are using the wrong format string. For long int you probably should use %ld .
If the programmer uses a conversion code with the wrong type of variable, then some strange thing will be seen on the screen and the error often propagates to other items in the printf () list. Every time it gives different output. Here, ch is a variable of string but we use %d which is generally used for the int data type.
Unlike C, which starts counting at 0 (array elements :-), gcc starts counting function arguments at 1. So in printf (fmt, value)
, "argument 1" would refer to fmt
, and "argument 2" refers to value
. Easy, isn't it?
As for the correct integer type to printf an __off_t
, there currently is no 100% guaranteed and portable way. Your best bet is to cast it to the widest unsigned type your implementation supports. Note that an unsigned long
may only be 32 bits wide and you'd get problems with files >= 4GB. If you have a C99 implementation, or if it supports unsigned long long
, 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 matching other POSIX types like off_t
, pid_t
etc. Once that is out the door (don't hold your breath), printing file sizes will be a little more portable and elegant.
I receive the abover error message but i do not understand why. I am under the impression that i am only passing one argument for both
st_size
andst_atime
.
But you are passing two arguments to printf
,
The second argument is of type __off_t
, but the format is for int
arguments. What the correct format for __off_t
is, I don't know, %ld
or %zd
have a good chance of being correct, but to play it safe, cast to intmax_t
and use %jd
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With