Q:

Input integer, float and character values using one scanf() statement in C

belongs to collection: C scanf() Programs

0

Input integer, float and character values using one scanf() statement in C

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

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 ("%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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Input an integer value and print with padding by Z... >>