18

I am trying to compile the below on RHEL 5.6 , 64 bit, and i keep getting a warning

"var.c:7: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘long unsigned int’"

Here is my code:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    unsigned int n =10;
    printf("The size of integer is %d\n", sizeof(n));
}

It does not matter if i change the declaration for "n" to following

  1. signed int n =10;
  2. int n = 10;

All i want to do is print the size of integer on my machine, without really looking into limits.h.

Robert Groves
  • 7,114
  • 5
  • 34
  • 49
Jimm
  • 7,173
  • 14
  • 59
  • 106
  • 2
    possible duplicate of [Platform independent size_t Format specifiers in c?](http://stackoverflow.com/questions/2125845/platform-independent-size-t-format-specifiers-in-c/22114959#22114959) – maxschlepzig Mar 01 '14 at 13:29
  • Possible duplicate of [Cross platform format string for variables of type size\_t?](http://stackoverflow.com/questions/174612/cross-platform-format-string-for-variables-of-type-size-t) – Ciro Santilli新疆棉花TRUMP BAN BAD Oct 02 '15 at 09:34
  • Possible duplicate of [What's the correct way to use printf to print a size\_t?](http://stackoverflow.com/questions/940087/whats-the-correct-way-to-use-printf-to-print-a-size-t) – phuclv May 06 '17 at 09:12

3 Answers3

46

The sizeof function returns a size_t type. Try using %zu as the conversion specifier instead of %d.

printf("The size of integer is %zu\n", sizeof(n));

To clarify, use %zu if your compiler supports C99; otherwise, or if you want maximum portability, the best way to print a size_t value is to convert it to unsigned long and use %lu.

printf("The size of integer is %lu\n", (unsigned long)sizeof(n));

The reason for this is that the size_t is guaranteed by the standard to be an unsigned type; however the standard does not specify that it must be of any particular size, (just large enough to represent the size of any object). In fact, if unsigned long cannot represent the largest object for your environment, you might even need to use an unsigned long long cast and %llu specifier.

In C99 the z length modifier was added to provide a way to specify that the value being printed is the size of a size_t type. By using %zu you are indicating the value being printed is an unsigned value of size_t size.

This is one of those things where it seems like you shouldn't have to think about it, but you do.

Further reading:

Jaakko
  • 3,154
  • 20
  • 17
Robert Groves
  • 7,114
  • 5
  • 34
  • 49
4

Your problem is that size_t is an unsigned type. Try using

printf("The size of integer is %u\n", sizeof(n));

and you should get rid of that warning.

Aurojit Panda
  • 909
  • 6
  • 11
  • Thank you, your explanation helps. – Jimm May 10 '11 at 00:30
  • 1
    Note that this is not portable to systems where `size_t` and `unsigned int` have different lengths. – Jim Balter May 10 '11 at 00:52
  • @Jim Balter: True, something like printf("The size of integer is %zu\n", sizeof(n)); on GCC or printf("The size of integer is %Iu\n", sizeof(n)); on MSVC are more portable, I however don't know of an easy way to write this for the general case. – Aurojit Panda May 10 '11 at 03:05
  • Since `sizeof(int)` must be a small integer, `%u` and casting to `(unsigned)` would do in this case. – Jim Balter May 10 '11 at 05:52
-5

I think you should write this instead:

printf("The size of integer is %d\n", sizeof(int));
SIFE
  • 5,203
  • 6
  • 30
  • 42
  • 1
    This still returns the warning var.c:7: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘long unsigned int’ – Jimm May 10 '11 at 00:28
  • @SIFE I'm curious as to why you think that would yield a different result. – Jim Balter May 10 '11 at 00:50
  • @Jim If I don't forget, this was the right way to see the size of types in C. – SIFE May 10 '11 at 20:30
  • @SIFE `sizeof(int)` and `sizeof(n)` (where `n` was declared as `int`) have the same value. You don't seem to grasp the issues here -- please see the other answers and comments so as to do so. – Jim Balter May 10 '11 at 20:59
  • @Jim I didn't say he is wrong, all I said how it should be wrote. – SIFE May 10 '11 at 22:34
  • @SIFE You're confused. Please read the other answers and comments. – Jim Balter May 10 '11 at 23:29