Inconsistent results from printf with long long int?

前端 未结 4 1575
花落未央
花落未央 2021-01-16 07:59
struct DummyStruct{
        unsigned long long std;
        int type;
};

DummyStruct d;
d.std = 100;
d.type = 10;

/// buggy printf, unsigned long long to int conve         


        
4条回答
  •  无人共我
    2021-01-16 08:16

    Its your usage that is the problem. Unless the types specified in the format string are exactly the same as the types in the parameters then things will not work correctly.

    This is because the compiler pushes the parameters as-is onto the stack.
    There is not type checking or conversion.

    At run-time the code is pulling the values of the stack and advancing to the next object based on the value in the format string. If the format string is wrong then the amount advanced is incorrect and you will get funny results.

提交回复
热议问题