1

I'm wondering what's the difference between %D, %d and %i in the printf function, because they all display integers, so why are there 3 formats to display integers? There must be a difference between these three formats?

Edit: I was also asking for %D, not only %i and %d.

Flaz
  • 115
  • 1
  • 1
  • 8
  • http://en.cppreference.com/w/c/io/fprintf – Oliver Charlesworth Jul 06 '17 at 14:54
  • There isn't. refer - http://www.cplusplus.com/reference/cstdio/printf/ – Suraj S Jul 06 '17 at 14:54
  • There's no `%D`, and the other two are *exactly equivalent* –  Jul 06 '17 at 14:55
  • It's funny, that both the linked references for the C library functions come from C++ sites. – Baldrickk Jul 06 '17 at 14:57
  • 2
    @BaldrickkL: en.cppreference.com covers C where stated. cplusplus.com is a load of tosh. – Bathsheba Jul 06 '17 at 14:57
  • Possible duplicate: https://stackoverflow.com/questions/17329647/i-or-d-to-print-integer-in-c-using-printf – Sofo Gial Jul 06 '17 at 14:58
  • Possible duplicate of [%i or %d to print integer in c using printf()?](https://stackoverflow.com/questions/17329647/i-or-d-to-print-integer-in-c-using-printf) – Sofo Gial Jul 06 '17 at 14:59
  • No, not good duplicates, due to the OP also asking about %D. – Bathsheba Jul 06 '17 at 14:59
  • 1
    It may be worth noting that the conversion specifiers for `scanf()` behave differently than the corresponding specifiers for `printf()`; in particular, `%i` and `%d` are not quite the same when used with `scanf()`. – ad absurdum Jul 06 '17 at 15:00
  • @SurajS: Don't post links to different languages, even if that is the C library. C and C++ are different languages! – too honest for this site Jul 06 '17 at 15:11
  • Acknowledged. @Olaf – Suraj S Jul 06 '17 at 15:15
  • 1
    I find the Linux man pages project best reference for standard C and POSIX.1 functions; for [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) in this case. The *Conforming to* section tells you which features are defined in which standard, so the only thing "Linux" there is that it is mostly used by Linux distributions. Don't expect those Microsoft inanities defined in C11 like `scanf_s()` to be documented there, however. (Only C programmers working exclusively on Windows use those. For actually portable code, look into POSIX.1 instead.) – Nominal Animal Jul 06 '17 at 20:08

2 Answers2

7

First, %D isn't a standard.

When it comes to %d and %i, there's no difference for output (e.g: printf), as pointed by some users on the comments and Oliver Charlesworth on his answer.

However, for input data (e.g.: scanf) you can use %i for scanning hexadecimal values (if preceded by 0x), or octal (if preceded by 0). Its default behavior will scan decimal values.

E.g: if you input some data using %i like 0x28, it will be the same as 40 in dec.

EDIT: Some code as example:

#include <stdio.h>
int main(){
    int dec, hex;
    scanf("%i",&hex); //For example, hex = 0x28
    scanf("%d",&dec); //For example, dec = 28
    printf("Hex: %d\nDec: %d\n",hex,dec); // outputs: Hex = 40, Dec = 28
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
woz
  • 544
  • 6
  • 22
  • Okay thanks. %D works for me, so I thought it was a standard like %i and %d. – Flaz Jul 06 '17 at 15:05
  • 4
    @Flaz, not only is `%D` nonstandard, but different C libraries handle it differently. Do not use it. Examine your `printf()` calls to see what data types are actually being provided, and replace `%D` with a directive appropriate for the corresponding type (possibly `%ld`, but check). – John Bollinger Jul 06 '17 at 15:11
  • 2
    @woz ... 028 is octal, hex shall be 0x28! – Sir Jo Black Jul 06 '17 at 15:13
  • 2
    Like @SergioFormiggini said, in scanf, %i will interpret 028 in octal and will assign 2 to the variable leaving 8 for later consumption. 0x28 would be interpreted as hex and assign 40 to the variable. – Jonathan Leffler Jul 06 '17 at 15:34
  • 1
    @SergioFormiggini thanks! Note to myself: do not code/answer before lunch. – woz Jul 06 '17 at 15:36
  • NOTE that 028 is a wrong octal, since 8 is not a valid digit in the 8-base (octal) numeration. As @JonathanLeffler said, 8 remains into the input buffer. – Sir Jo Black Jul 06 '17 at 15:43
4

There is no difference between %d and %i, and as far as I can tell, %D isn't a thing (or at least, maybe it's a compiler-specific extension).

See http://en.cppreference.com/w/c/io/fprintf, or section 7.19.6.1 of the e.g. the C99 standard.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680