Home » C programs » C scanf() 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?
Submitted by IncludeHelp, on September 11, 2018

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.

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

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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.