如何输入超过1个字符并仍然逐步执行程序

如何输入超过1个字符并仍然逐步执行程序

本文介绍了如何输入超过1个字符并仍然逐步执行程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我输入下面的代码并尝试输入我的名字时,其余的代码执行自己而不允许我输入任何数据但是当我为名称输入单个字符时,我可以逐个输入数据。这是为什么 ?



When I enter the code below and try to enter my name then rest of the codes executive themselves without allowing me to enter any data but when I enter a single character for name then I can input the data one by one. Why is that ?

#include <stdio.h>

int main() {

	char name;
	int age;
        char gen;

	printf("What is your name ? \n");
	scanf(" %c", &name);

	printf("What is your age ? \n");
	scanf(" %i", &age);

	printf("What is your gender ? \n");
	scanf(" %c", &gen);

	printf("Thank you for the information! :)");
	return 0;
}





我的尝试:



我尝试过使用char name [10];我认为它可能是存储问题,我也将10改为1000但它似乎不起作用。每当我输入超过1个字符时,程序执行代码本身的其余部分。



What I have tried:

I have tried using "char name[10];" thinking it might be the storage problem, I have also changed "10" to "1000" but it doesn't seem to work. Whenever I input more than 1 character, the program executes the rest of the code itself.

推荐答案


Quote:

我尝试过使用char name [10];认为它可能是存储问题,

I have tried using "char name[10];" thinking it might be the storage problem,

存储问题。在 C 编程语言中, char 数据类型表示单个字符。另一方面,字符串(例如名称)是( 0 已终止) char acters。例如,请参阅 []。

There is also a storage problem. In C programming language, char data type represents a single character. On the other hand, strings (e.g. names) are (0 terminated) array of characters. See, for instance, C Programming Strings[^].


这篇关于如何输入超过1个字符并仍然逐步执行程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 05:22