c语言scanf输入字符_在C语言中使用一个scanf()语句输入整数,浮点和字符值

c语言scanf输入字符

We have to read tree values: integer, float and then character using only one scanf() function and then print all values in separate lines.

我们只需要使用一个scanf()函数读取树值:整数,浮点数和字符,然后将所有值打印在单独的行中。

Example:

例:

    Input:
    Input integer, float and character values: 10 2.34 X

    Output:
    Integer value: 10
    Float value: 2.340000
    Character value: X

Before moving to the program, please consider the scanf() statement,

移至该程序之前,请考虑scanf()语句,

    scanf ("%d%f%*c%c", &ivalue, &fvalue, &cvalue);

Here, %*c is used to skip the character to store character input to the character variable, almost all time, character variable’s value will not be set by the given value because of the "ENTER" or "SPACE" provided after the float value, thus, to set the given value, we need to skip it.

此处, %* c用于跳过字符以将输入的字符存储到字符变量中,几乎所有时间,由于在浮点值之后提供“ ENTER”“ SPACE” ,因此将不会通过给定值设置字符变量的值,因此,要设置给定值,我们需要跳过它。

To learn more, read: Skip characters while reading integers using scanf() in C

要了解更多信息,请阅读: 在C语言中使用scanf()读取整数时跳过字符

Program:

程序:

# include <stdio.h>

int main ()
{
	int ivalue;
	float fvalue;
	char cvalue;

	//input 
	printf("Input integer, float and character values: ");
	scanf ("%d%f%*c%c", &ivalue, &fvalue, &cvalue);

	//print 
	printf ("Integer value: %d\n", ivalue) ;
	printf ("Float value: %f\n", fvalue) ;
	printf ("Character value: %c\n", cvalue) ;

	return 0;
}

Output

输出量

Input integer, float and character values: 10 2.34 X
Integer value: 10
Float value: 2.340000
Character value: X


翻译自: https://www.includehelp.com/c-programs/input-integer-float-and-character-values-using-one-scanf-statement.aspx

c语言scanf输入字符

你可能感兴趣的:(c语言scanf输入字符_在C语言中使用一个scanf()语句输入整数,浮点和字符值)