Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C format issue with printf("%d", astatbuff->st_size); [duplicate]

Tags:

c

format

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.

like image 530
user2112062 Avatar asked Mar 01 '13 20:03

user2112062


People also ask

How to use printf to print a size_t in C/C++?

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.

What is formatted printing in C?

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.

Why can't I use%LD in printf statements?

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 .

What happens if you use the wrong type of variable in printf?

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.


2 Answers

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.

like image 140
Jens Avatar answered Oct 25 '22 08:10

Jens


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 and st_atime.

But you are passing two arguments to printf,

  1. the format string
  2. the struct member

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.

like image 34
Daniel Fischer Avatar answered Oct 25 '22 08:10

Daniel Fischer