c - 是否可以对同一个变量使用 scanf 两次?
问题描述
是否可以提示用户再次输入一个字符并让新字符替换原来的字符?当我尝试这个时,它不允许我第二次输入字符。
#include <stdio.h>
int main(){
char x;
printf("enter value: ");
scanf("%c", &x);
printf("enter value: ");
scanf("%c", &x);
}
解决方案
#include<stdio.h>
int main(){
char x='p';
printf("enter value: ");
scanf("%c", &x);
printf("val is %c \n",x);
printf("enter 2nd value: ");
getchar();//flush the buffer
scanf("%c", &x);
printf("val is %c \n",x);
}
问题是您没有清除缓冲区 getchar 函数将清除它。
推荐阅读
- python - 如何动态定义scrapy项目
- embedded-linux - 设备树:如何更改子设备节点的属性
- c - 如何减少花在一条指令上的时间?
- onem2m - 控制 oneM2M 中的灯组
- concurrency - 如何让雅典娜同时处理多个查询
- python - 为什么python中的list属性取对象列表中所有对象的所有值?
- node.js - Express 保留一个请求,直到最后一个请求完成
- jquery - 在进行 AJAX 发布之前如何使用 Google Recaptcha
- node.js - 定义 docker-compose 文件的 IP
- php - 如何通过将键与字符串进行比较以获取值来获取php关联数组中键的值?