Examples of simple scanf() function calls
#include <stdio.h>
int main(void)
{
int a, b, c;
float x, y, z;
char initial, letter, alpha;
printf("\nEnter 1 integer ");
scanf("%d", &a);
printf("\nEnter 2 integers ");
scanf("%d%d", &b, &c);
printf("\nYou entered %d %d %d\n", a, b, c);
printf("Enter 1 float ");
scanf("%f", &x);
printf("Enter 2 floats ");
scanf("%f%f", &y, &z);
printf("\nYou entered %f %f %f\n", x, y, z);
printf("\nEnter 1 character ");
scanf("%c", &initial);
printf("\nEnter 2 characters ");
scanf("%c%c", &letter, &alpha);
printf("\nYou entered %c for initial %c for letter and %c for alpha\n",
initial, letter, alpha);
/*Actual code has a line here that clears out the input stream which
I did not include with this example. It'll show up when we do loops.*/
printf("\nChanged input = added a space ");
printf("\nEnter 1 character ");
scanf(" %c", &initial);
printf("\nEnter 2 characters ");
scanf(" %c %c", &letter, &alpha);
printf("\nYou entered %c for initial %c for letter and %c for alpha\n",
initial, letter, alpha);
return (0);
}
Output for the above program
Enter 1 integer 1
Enter 2 integers 5 9
You entered 1 5 9
Enter 1 float 1.2 2.3 4.5
Enter 2 floats
You entered 1.200000 2.300000 4.500000
Enter 1 character
Enter 2 characters a b
You entered
for initial a for letter and for alpha
Changed input = added a space
Enter 1 character b
Enter 2 characters c d
You entered b for initial c for letter and d for alpha