×

C Programs

C Basic & Conditional Programs

C Looping Programs

C String Programs

C Miscellaneous Programs

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

Here, we are going to learn how to input an integer, a float and a character value using single scanf() function in C programming language?
By IncludeHelp Last updated : March 10, 2024

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

scanf() statement

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.

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

C program to input integer, float and character values using one scanf()

# 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

C scanf() Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.