lunes, 14 de marzo de 2022

Difference between %d and %i format specifier in C language

https://www.geeksforgeeks.org/difference-d-format-specifier-c-language/

Difference between %d and %i format specifier in C language


%d and %i behavior is different in scanf

%d assume base 10 while %i auto detects the base. Therefore, both specifiers behaves differently while they are used with an input specifier. So, 012 would be 10 with %i but 12 with %d. 

  • %d takes integer value as signed decimal integer i.e. it takes negative values along with positive values but values should be in decimal otherwise it will print garbage value.( Note: if input is in octal format like:012 then %d will ignore 0 and take input as 12) Consider a following example. 
     
  • %i takes integer value as integer value with decimal, hexadecimal or octal type. 
    To enter a value in hexadecimal format – value should be provided by preceding “0x” and value in octal format – value should be provided by preceding “0”. 

No hay comentarios:

Publicar un comentario