c - c1 如何在 va_arg 中获取这个值?

标签 c variables variadic-functions var

我不明白 c1 如何打印 65, 66, 67, 68。我知道 char c 如何打印 A B C D,但是 c1 怎么有奇怪的输出?

    #include<stdio.h>
    #include<stdarg.h>
    void display(int num, ...);

    int main()
    {
      display(4, 'A', 'B', 'C', 'D');
      return 0;
    }
    void display(int num, ...)
    {
      char c, c1; int j;
      va_list ptr, ptr1;
      va_start(ptr, num);
      va_start(ptr1, num);
      for(j=1; j<=num; j++)
       {
        c = va_arg(ptr, int);
        printf("%c", c);
        c1 = va_arg(ptr1, int);
        printf("%d\n", c1);
       }
     }

最佳答案

这并不是一个奇怪的输出。您已在 printf("%d\n", c1); 中提到格式说明符为“%d”。 “%d”是整数格式说明符。因此,变量 c1 被隐式类型转换为整数,即字符的 ASCII 值。因此,在您的情况下,它将“A”打印为 65,“B”打印为 66 等。

请参阅 fprintf() 中的转换说明符主题

关于c - c1 如何在 va_arg 中获取这个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56537509/

相关文章:

c - 为什么我的C工程编译找不到我的结构?

c - 即使没有错误,为什么我没有得到所需的输出?

c - 在 Linux 上,\n 在使用 gcc 编译器的 gdb 中被视为两个字符

php - "Notice: Undefined variable"、 "Notice: Undefined index"、 "Warning: Undefined array key"和 "Notice: Undefined offset"使用 PHP

bash - 从 Bash 中的配置文件解析变量

html - sass 增量@loop 0.25,0.50,0.75,1

c++ - 在Matlab中将固定长度的字节数组写入二进制文件

objective-c - 带有 NS_REQUIRES_NIL_TERMINATION 的多参数列表

c++ - 解压函数调用的参数数组

java - slf4j 是否有 Java 1.5 varargs API?