C语言中getchar的printf格式字符

问题描述

我的任务是打印ASCII码编号和输入: 下面是我的代码:

#include <stdio.h>

int main(void){
  char ch;
  printf("Whatever: ");
  while((ch = getchar()) != 'q')
  {
    getchar();
    printf("The char is %c and %d in ASCII.\n",ch,ch);
  }

  return 0;
}

如果要从getchar()函数打印\ n,我应该修改什么?

Example: 
  The char is \n and 10 in ASCII.

解决方法

希望以下帮助

while((ch = getchar()) != 'q')
{
   if(ch == '\n')
        printf("The '\\n' ascii value is %d\n",ch);
   else
        printf("The '%c' ascii value is %d\n",ch,ch);
}

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...