Thread: Understanding Conversion Specifications!

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    34

    Unhappy 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. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    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;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Nov 2017
    Posts
    34
    Thanks Man, you are a life saver!! Kudos man

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need c programming code for the following specifications
    By VNS Sarvani in forum C++ Programming
    Replies: 3
    Last Post: 11-02-2014, 11:41 AM
  2. File path specifications
    By Shingetsu Kurai in forum C# Programming
    Replies: 6
    Last Post: 09-20-2011, 05:56 AM
  3. Exception specifications: throw() useful?
    By Memloop in forum C++ Programming
    Replies: 7
    Last Post: 11-25-2009, 02:24 AM
  4. Obtaining System Specifications Through C++
    By SiLeNt BoB in forum C++ Programming
    Replies: 4
    Last Post: 06-06-2003, 08:40 AM
  5. Exception specifications: good or bad?
    By Just in forum C++ Programming
    Replies: 0
    Last Post: 02-19-2003, 09:19 PM

Tags for this Thread