What is the difference between %i and %d in scanf() with C

1 Answer

0 votes
#include <stdio.h> 
  
int main() 
{ 
    int a, b, c;
    
    scanf("%i %i %i", &a, &b, &c);
    printf("a = %i b = %i c = %i\n", a, b, c); // take (base 10), hex (base 16) and octal (base 8)

    a = b = c = -1;
    
    scanf("%d %d %d", &a, &b, &c);
    printf("a = %d b = %d c = %d\n", a, b, c); // take only base 10 
    
    return 0; 
}



// 15 (base 10) = 15 (base 10)
// 0x15 (base 16) = 21 (base 10)
// 015 (base 8) = 13 (base 10)



/*
run:

15 0x15 015
a = 15 b = 21 c = 13
15 0x15 015
a = 15 b = 0 c = -1

*/

 



answered Apr 1, 2024 by avibootz
edited Apr 1, 2024 by avibootz

Related questions

1 answer 65 views
1 answer 79 views
1 answer 70 views
1 answer 227 views
...