Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   %i and %d place holders. (http://www.go4expert.com/forums/i-d-holders-t16087/)

Player 5Feb2009 21:31

%i and %d place holders.
Hi Guys.

Another quick question for you. I have just come across the %i place holder but i don't understand the difference between %i and %d. The section in my book that lists all the place holders says there both int place holders. In the program i am looking at it don't explain why the %i is used instead of %d. It's the first time i have come across the %i place holder. Sorry if it's a dumb question but i need to know everything :)

asadullah.ansari 6Feb2009 10:14

Re: %i and %d place holders.
% i attempts to take as integer.
%d attempts to take decimal integer.

asadullah.ansari 6Feb2009 10:26

Re: %i and %d place holders.
This example makes you more understand...

Code: c

#include <stdio.h>

int main()
   char InStr[] = "150 0125";
   int val1=0, val2=0;

   //******* Reading integer values*****///
    sscanf(InStr, "%i %i ", &val1, &val2);

    printf("The Decimal values are : %d %d\n", val1,val2);

    //******* Reading Decimal integer values*****///
     sscanf(InStr, "%d %d", &val1, &val2);

    printf("The Decimal values are : %d %d\n", val1,val2);
    return 0;


asadulla ~/temp> gcc test1.cc
execution gcc-3.3.1
asadulla ~/temp> ./a.out
The Decimal values are : 150 85
The Decimal values are : 150 125

All times are GMT +5.5. The time now is 19:04.