c - scanf 中 *variable 的用法以及为什么使用它?

标签 c scanf format-specifiers

你好,我是一个初学者,我一直在代码中看到这个,但我无法弄清楚它到底意味着什么...... 就像下面的例子

#include<stdio.h>

int main(void)
{
    char c1;
    do
    {
        scanf_s("%c%*c", &c1, 1);
        if ((c1>='a' && c1<='z')||(c1 >= 'A' && c1 <= 'Z'))
        {
            printf("%c  %d", c1, c1);
        }

    } while (c1!=0);
    return 0;
}

像这里一样它转换字符还是存储它的值? scanf 中的 (%*c) 对我来说是一个谜,据我所知,它有点对内存的引用,但不启动任何变量,它只是指向内存中存储的值...... 我说得对吗???

最佳答案

来自 C 标准(7.21.6.2 fscanf 函数)

3....After the %, the following appear in sequence:

An optional assignment-suppressing character *.

— An optional decimal integer greater than zero that specifies the maximum field width (in characters).

— An optional length modifier that specifies the size of the receiving object.

— A conversion specifier character that specifies the type of conversion to be applied.

和(K.3.5.3.2 fscanf_s 函数)

4 The fscanf_s function is equivalent to fscanf except that the c, s, and [ conversion specifiers apply to a pair of arguments (unless assignment suppression is indicated by a *)....

所以在这次通话中

scanf_s("%c%*c", &c1, 1);

格式%*c禁止将读取的字符分配给参数。通常这种方法用于从缓冲区读取用户按 Enter 键后出现的换行符。

关于c - scanf 中 *variable 的用法以及为什么使用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58803696/

相关文章:

c++ - 无法从 MSVCRT strtod/sscanf/atof 函数中获取 NaN

c - %02x 字符数组的格式说明符

c - sscanf ("0X80.9", "%f", &value) 将值设置为十六进制 float 而不是 float

c++ - 使用 libevent 或 boost::asio 在单线程中建立多个 tcp 连接。这是可能的?

c - 如何打印这个系列?(1\n 2 3\n 4 5 6\n 7 8 9 10 ... ... ...)

c - Mex 内存泄漏..我需要一些帮助来发现错误

c - 使用 printf 在 C 中打印 float / double 时指定精度

c - 如何优化图像像素化程序

c - C 中 scanf 和字符串的段错误

c - C 中使用 scanf 读取和存储整数