Jayadithya Shravage
PRO
2 months ago
Jayadithyacountry asked

I understand we use %d to print numbers, but does that mean the sizeof() operator doesn’t return a proper type on its own? Also, are there any cases where it's useful to use a different format specifier than the one matching the variable type?

Kelish Rai
Expert
last month
Kelish Rai answered

Hi Jayadithya,

Great question — this trips up a lot of learners.

The sizeof() operator does return the correct value — it gives you the size of a type or variable in bytes. But here’s the key part:
printf() doesn’t know what type you're printing unless you tell it using a format specifier.

So even if sizeof(int) returns a valid number like 4, you still need to use %d or %lu (depending on your system and compiler) to tell printf how to interpret and display that value.

Now, about using different format specifiers — yes, that’s sometimes intentional. For example:

char ch = 'A';
printf("%d", ch);  // Prints 65 instead of 'A'

Here, %d is used to show the numeric (ASCII) value of the character, not the character itself.

So to sum up:

  • sizeof() works fine — the issue is with how you choose to print the result.

  • Sometimes, using a "wrong" format specifier is done on purpose to view data differently.

If you have more questions, I am here to help.

C
This question was asked as part of the Learn C Programming course.