Thread: Understanding Conversion Specifications!

1. Understanding Conversion Specifications!

What's the use of %h , %u , %i when we can simply use %d for all the purposes.

For example;

Code:
```#include <stdio.h>

int
main() {
printf("%h",78);
printf("%d",78);
printf("%i",8909);
printf("%d",8909);
printf("%u",65530);
printf("%d",65530);
}```
as we can see we can use %d in place of %h,%i,%u , i know that they are used as modifiers for short int(h) , decimal , octal , hexadecimal interger(i) , and unsigned decimal integer(u)

so what's there purpose? I can't find any benefit or distinction in their usage. Please help!!

2. For the printf family of functions, %i is exactly the same as %d. However, for the scanf family, %i accepts octal input (with a leading 0) and hex input (with a leading 0x), whereas %d is only for decimal. So entering 077 will yield the decimal value 77 with %d but the decimal value 63 with %i.

%h is again important for scanf but not for printf. The reason it's not important for printf is that, due to printf being a variadic function, any char or short passed to it is converted to an int before being passed.

%u is for unsigned ints whereas %d is for signed ints, so that's obviously a difference as shown by the following:
Code:
```#include <stdio.h>
#include <limits.h>

int main() {
unsigned u = UINT_MAX;
printf("%u\n", u); // prints 4294967295 (assuming 32-bit ints)
printf("%d\n", u); // prints -1
return 0;
}```

3. Thanks Man, you are a life saver!! Kudos man

Popular pages Recent additions