Scanf int8_t corrupts stack

How to scan f int8_t and other types without this error. I used "cinttypes" to get the template constants, but that didn't help.

#include <cstdio>
#include <cstdint>
#include <cinttypes>

int main()
{
    int8_t var;
    scanf("%" SCNi8, &var);
    printf("%" PRIi8 "\n", var);
    return 0;
}

PS This error only occurs in Debug when created in Release Release.

PPS Output:

1>------ Build started: Project: SCANF_PROBLEM, Configuration: Debug Win32 ------
1>  SCANF_PROBLEM.cpp
1>d:\study\scanf_problem\scanf_problem\scanf_problem.cpp(11): warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          d:\visual studio\vc\include\stdio.h(283) : see declaration of 'scanf'
1>  SCANF_PROBLEM.vcxproj -> D:\Study\SCANF_PROBLEM\Debug\SCANF_PROBLEM.exe
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
+4
source share
2 answers

You have encountered a Microsoft C / C ++ runtime library error, cf. http://mfctips.com/tag/format/ or https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63417 (which reports this error for gcc under mingw, which is associated with Microsft libraries).

"% hhd" just doesn't work; you have to program around it (which is not too difficult, but sad).

MS (.. , ).

+1

C99 , , , ... , scanf . . n1256 7.8.1p6 :

, <stdint.h>, fprintf fscanf fscanf .

++, ... ++ 11 , <cstdint> C99s <stdint.h>, , C99.

: Microsoft C99, "N/A" , .

, C99 , ++ 11 , , C99 . SCNi8 , scanf ( , C99/++ 11), ... , , , .

Protip: .c, Microsoft Visual Studio C89. , . MSV++ C99 , ++ 11. , , , / ..., .

Protip # 2: LLVM/Clang Visual Studio. , , C99 (, Microsoft).

Protip # 3: printf...

SCNi8 PRIi8

Protip # 4: . 191 :

++ __STDC_FORMAT_MACROS <inttypes.h>.

+2

All Articles