illustration of %i specifier

Gaiety 1 Tallied Votes 157 Views Share

/*
difference between %i ad %d

%i reads the value and it recognizes the value as what base
it is when we read the value as 023 (base 8) it is 19 (base 10)
and 0xa(base 16) it is 10 in (base 10).
*/


OUTPUT:

/*
Enter a number:023
Out put:%d=19, %i=19,%x=17

Enter a number:0xa
Out put:%d=10, %i=10,%x=a
*/

int main()
{
        int num;
        printf("Enter a number:");
        scanf("%i",&num);
        printf("\nOut put:%%d=%d, %%i=%i,%%x=%x",num,num,num);
        return 0;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

But note that %i used by scanf() is different than %i used by printf(). For printf(), %i and %d are the same thing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.