【问题标题】:program produces numbers when I expected alpha characters当我期望字母字符时,程序会产生数字
【发布时间】:2020-05-22 15:07:32
【问题描述】:

我的代码:

#include <stdio.h>
int main()
{
  char x, y;
  for(x='a';x<='c';++x)
  {
    for(y='a';y<='c';++y)
    {
      printf("%c %d\r\n",x,y);
    }
  }
  return(0);
}

我想要: 一个 一个 一个 一个 b b ...等

我得到: 97 98 99 b 97 b 98 ...等

我错过了什么?

【问题讨论】:

  • 你在 %d 里面有一个 printf
  • @PiRocks 不,不会。 char 值将经过默认参数提升为 int。 (事实上​​,%d%c 都需要 int 类型的参数。)
  • @PiRocks — 不会。在printf() 的调用中,xy 中的值被提升为int,因为printf() 原型末尾的省略号, ...)

标签: c loops nested


【解决方案1】:

这是我的第一个程序和第一次编译。感谢您的回复。 我改变了一行:

printf("%c %d\r\n",x,y);

到:

printf("%c %c\r\n",x,y);

我有很多东西要学:/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-25
    • 1970-01-01
    • 2020-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多