-1
#include<stdio.h>
int main() {
    int d = -8623;
    printf("|%6D|", d);
    return 0;
}

I used my pc and ran it,the result is:|D|,but the standard answer is:|%6D|,same with online IDE.

  • 1
    The call of printf has undefined behavior. To get the "standard answer" you should use the string literal "|%%6D|" – Vlad from Moscow Nov 07 '19 at 15:22
  • If your compiler didn't warn you about this, turn up the warning options (`-Wall -Wextra` is good for gcc and clang) – Shawn Nov 07 '19 at 15:25
  • What are you actually trying to do here? If you want to print the literal string `"|%6D|"`, why are you passing the variable `d`? If you want to print `d`'s value, why are you using `%D`? – Steve Summit Nov 07 '19 at 15:25

3 Answers3

1

Replace

printf("|%6D|", d);

with

printf("|%d|", d);

%d is correct format for signed integers. Local PC and online IDE answers differ because you invoked undefined behavior.

SurvivalMachine
  • 7,946
  • 15
  • 57
  • 87
  • What output do you expect with your solution and how do you think that answers the question? – Yunnosch Nov 07 '19 at 15:24
  • But how to explain this code is not the same result on online IDE and local pc? – End_nether Nov 07 '19 at 15:25
  • @End_nether I improved my answer – SurvivalMachine Nov 07 '19 at 15:26
  • @End_nether Different compilers will give different results where behavior is undefined. You're using a format specifier (`%6D`) that doesn't match anything defined in the standard, so different implementations of `printf()` will give different results. The key to this answer is that it replaces your incorrect specifier with one that's defined in the standard, so all standards-compliant libraries will give the same result. – Caleb Nov 07 '19 at 15:30
  • OK,thx! Now I understand it. – End_nether Nov 07 '19 at 15:31
  • Worth noting that *some* compilers recognize `%D` as a format specifier. – Eugene Sh. Nov 07 '19 at 15:34
0

The call of printf has undefined behavior because there is used an incorrect format specifier.

To get the "expected result" you should write at least

printf("|%%6D|", d);

though the compiler can report an error because in the function call there is unused argument d.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

Why is it not the same result in this code?

How a program written in C should behave after it's compiled and executed is described in the C standard.

The draft of the standard, to which we have free access, states in C11 7.21.6.1p6 that if a printf conversion specification is invalid, the "behavior is undefined". The %D is an invalid conversion - there is no D conversion specifier listed in (f)printf description.

The term undefined behavior is defined as in C11 3.4.3p1:

behavior, upon use of a nonportable or erroneous program construct or of erroneous data, for which this International Standard imposes no requirements

There are no requirements on the code you presented. It can behave in any way, however it wants - print anything, start world war 3 or spawn dragons. More about undefined behavior can be found in this stackoverflow thread.

In case of undefined behavior in your program, anything can happen and there is no "standard answer". The result of the execution are unpredictable and can change with environment or compiler or compiler version. There is nothing strange that different environments behave differently with code with undefined behavior.

The difference you see comes from the difference in implementation of printf on different platforms you tested. Your local printf implementation differs from the printf implementation available in the "online IDE" you've tried, that's why you are getting different results.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • Do not misunderstand the C standard. It does not say how C programs should behave. It says **some** of how C programs executed in implementations **that conform to the standard** behave. As the standard says, when **it** does not define the behavior, that means **this International Standard** imposes no requirements. That does not mean there are no requirements on the code. It is false that “anything can happen.” – Eric Postpischil Nov 07 '19 at 15:42