NO

Author Topic: Why these warnings for printf?  (Read 2093 times)

PabloMack

  • Guest
Why these warnings for printf?
« on: March 14, 2019, 12:39:16 AM »
  printf("Size of int is: %u\n",sizeof(int));
  printf("Size of ptr is: %u\n",sizeof(&Mod));

E:\Pelles\a\a.c(146): warning #2234: Argument 2 to 'printf' does not match the format string; expected 'int' but found 'unsigned long long int'.
E:\Pelles\a\a.c(147): warning #2234: Argument 2 to 'printf' does not match the format string; expected 'int' but found 'unsigned long long int'.

They seem to be quite correct to me. sizeof should produce an int. I've never had a problem with a statement like this with any other compiler.
If I cast sizeof to an int then the warnings go away as in:

  printf("Size of int is: %u\n",(int)sizeof(int));
  printf("Size of ptr is: %u\n",(int)sizeof(&Mod));

It doesn't make sense for sizeof to produce a long long.
« Last Edit: March 14, 2019, 12:42:18 AM by PabloMack »

Offline DMac

  • Member
  • *
  • Posts: 272
Re: Why these warnings for printf?
« Reply #1 on: March 14, 2019, 05:46:29 AM »
size_t is unsigned int for 32bit compiler and it is unsigned long long for 64bit compiler.
No one cares how much you know,
until they know how much you care.

PabloMack

  • Guest
Re: Why these warnings for printf?
« Reply #2 on: March 14, 2019, 06:10:55 PM »
Can I redefine size_t or is it hard-coded in the compiler?

The AMD64 architecture is a bit strange in that it is not fully a 64-bit system as offsets are at most 32 bits. (If you can't reach them, what good are they?) And because offsets are signed, that means size of anything using the usual addressing modes can't be larger than can be expressed in 31-bits. You can operate on 64-bit values but you can't easily access members within data structures that have a size larger than can be expressed in 31-bits. The AMD64 is like a 32-bit architecture with a 64-bit address space and some 64-bit operations thrown in. It is reminiscent of 8-bit data architectures that have 16-bit address spaces.

So, for the AMD64, it makes the most sense for size_t to be 32-bits. Of course you can "manually" do the 64-bit math that is necessary to use very large data items as you can do in 8-bit systems with 16-bit address spaces. But the same could be simulated for a system with a 128-bit address space but we won't go there.
« Last Edit: March 14, 2019, 06:31:35 PM by PabloMack »