悝跞 2022-02-04 11:25 采纳率: 100%
浏览 37
已结题

scanf引用第二个值跳转后第三个哪来的

#include"stdio.h"
int main()
{int a,b,c,d;
scanf("%2d%*3d%2d",&a,&b,&c);
printf("%d,%d,%d",a,b,c);}
输入:123456789
输出:12 67 127

  • 写回答

1条回答 默认 最新

  • _GX_ 2022-02-04 15:14
    关注

    %*3d表示忽略域宽为3个字符的整型,因此你输入的字符串中345是被忽略掉了,再接下来%2d读入67是赋给b的,c没有被赋值,其值是未初始化的随机值。

    FROM https://en.cppreference.com/w/c/io/fscanf

    conversion specifications. Each conversion specification has the following format:

    • introductory % character
    • (optional) assignment-suppressing character *. If this option is present, the function does not assign the result of the conversion to any receiving argument.
    • (optional) integer number (greater than zero) that specifies maximum field width, that is, the maximum number of characters that the function is allowed to consume when doing the conversion specified by the current conversion specification. Note that %s and %[ may lead to buffer overflow if the width is not provided.
    • (optional) length modifier that specifies the size of the receiving argument, that is, the actual destination type. This affects the conversion accuracy and overflow rules. The default destination type is different for each conversion type (see table below).
    • conversion format specifier
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 2月20日
  • 已采纳回答 2月12日
  • 创建了问题 2月4日