ExplodingFreeze's blog

By ExplodingFreeze, history, 2 years ago, In English

First of all apologies if this is something that has been asked before, my searching ability appears to have failed me then.

While using printf with formatting for zero padding on CF (including custom invocation), I encountered this strange behavior:

WA — 109283810

AC — 109284812

The only difference between these two submissions in the language chosen (32bit vs 64bit C++17). Somehow in the 32-bit version, the second argument is always processed as zero.

In the 32-bit C++17 version, splitting the printf into 2 printfs also causes it to get AC — 109284259

  1. Does CF use a different compiler / environment for 64 bit that could explain this difference in behavior?

  2. Is there some undefined behavior in my code that is the source of this inconsistency?

  • Vote: I like it
  • +8
  • Vote: I do not like it

2 years ago, # |
Rev. 2   Vote: I like it +61 Vote: I do not like it

%02d is for a signed integer (int). Since you have done #define int long long, your arguments are actually long longs. So, this is undefined behavior. You need to pass int arguments, or use the %lld format specifier for long long.

Be sure to enable compiler warnings with -Wall -Wextra, which can warn of such mistakes.