Home>
#include <
iostream >
#Include <
stdio.h >
#Include <
Windows.h >
Using Namespace STD;
INT MAIN ()
{
    SetConsoleCP (1251);
    SetConsoleoutPutcp (1251);
   Char M,
        n,
        L,
        w;
    unsigned __int16 unitstateword;
    PrintF ("Enter the word state of the device \ n");
    printf ("(16-rich number from 0 to 0xFFFF) >
");
    scanf_s ("% x", &
UnitstateWord);
    M= (UnitstateWord >
≫
 13) &
 7;
    n= (UnitstateWord >
≫
 10) &
 7;
    L= (UnitstateWord >
≫
 eight) &
 one;
    w= unitstateword &
 0xFF;
    Return 0;
}
c++ c
  • Answer # 1

    because

    unsigned __int16 unitstateword;
    

    implies 2 bytes in Unitstateword. , but "% x" -Four-proof integer. As a result, the recording goes in memory outside the variable and spoil the stack ...

    By the way, Vc++ speaks directly about it:

    t.cpp (18): Warning C4477: "scanf_s": In the formatting string "% x" requires an argument like "UNSIGNED INT *", but the variable argument "1" has the type "Unsigned short *"
    T.CPP (18): Note: It is recommended to use "% HX" in the formatting string

    What did you compile? Or just did not understand the message?

    Vc++ 2019, but there is nothing there is nothing

    Whooow2021-05-04 06:33:49

    How then correctly write scanf so that she takes 2 bye 16-riche number?

    Whooow2021-05-04 06:39:25

    Look at the last line in response. And turn on the warnings in the compiler!

    Harry2021-05-04 06:46:51

    I do not understand one thing here [cplusplus.com/reference/cstdio/scanf/] writes that for% x, h means unsigned short int *, but the length of unsigned short int * depends on the compiler, and it may differ

    Whooow2021-05-04 06:51:11

    In the __int16_t compiler you are using Short Int. However, you can use #include and the format string of SCNX16 defined in it for __int16.

    Harry2021-05-04 07:07:44
  • Answer # 2

    because

    unsigned __int16 unitstateword;
    

    implies 2 bytes in Unitstateword. , but "% x" -Four-proof integer. As a result, the recording goes in memory outside the variable and spoil the stack ...

    By the way, Vc++ speaks directly about it:

    t.cpp (18): Warning C4477: "scanf_s": In the formatting string "% x" requires an argument like "UNSIGNED INT *", but the variable argument "1" has the type "Unsigned short *"
    T.CPP (18): Note: It is recommended to use "% HX" in the formatting string

    What did you compile? Or just did not understand the message?

    Vc++ 2019, but there is nothing there is nothing

    Whooow2021-05-04 06:33:49

    How then correctly write scanf so that she takes 2 bye 16-riche number?

    Whooow2021-05-04 06:39:25

    Look at the last line in response. And turn on the warnings in the compiler!

    Harry2021-05-04 06:46:51

    I do not understand one thing here [cplusplus.com/reference/cstdio/scanf/] writes that for% x, h means unsigned short int *, but the length of unsigned short int * depends on the compiler, and it may differ

    Whooow2021-05-04 06:51:11

    In the __int16_t compiler you are using Short Int. However, you can use #include and the format string of SCNX16 defined in it for __int16.

    Harry2021-05-04 07:07:44