Advertisement
Formatted functions | Description |
---|---|
scanf() |
The scanf() function allows us to read one or multiple values entered by the user through the keyboard at the console. |
printf() |
The printf() function is used to print or display one or multiple values in the output to the user at the console. |
#include<stdio.h>
int main()
{
char ch;
int i;
float f;
double d;
long l;
/* Reading values using scanf() */
printf("Reading the values -");
printf("\n");
printf("Enter a char value : ");
scanf("%c",&ch); /* Reading a char value with format specifier %c */
printf("Enter an int value : ");
scanf("%d",&i); /* Reading an int value with format specifier %d */
printf("Enter a long value : ");
scanf("%ld",&l); /* Reading an long value with format specifier %ld */
printf("Enter a float value : ");
scanf("%f",&f); /* Reading an float value with format specifier %f */
printf("Enter a double value : ");
scanf("%lf",&d); /* Reading an double value with format specifier %lf */
/* Displaying values using printf() */
printf("Display the values you've entered -");
printf("\n");
/* Displaying a char value with format specifier %c */
printf("The char value you entered : %c", ch);
printf("\n");
/* Displaying an int value with format specifier %d */
printf("The int value you entered : %d", i);
printf("\n");
/* Displaying a long value with format specifier %ld */
printf("The long value you entered : %ld", l);
printf("\n");
/* Displaying a float value with format specifier %f */
printf("The float value you entered : %f", f);
printf("\n");
/* Displaying a double value with format specifier %lf */
printf("The double value you entered : %lf", d);
return 0;
}
Reading the values -
Enter a char value : a
Enter an int value : 10
Enter an long value : 12345678
Enter a float value : 3.4567
Enter a double value : 99.987654
Display the values you've entered
The char value you entered : a
The int value you entered : 10
The long value you entered : 12345678
The float value you entered : 3.456700
The double value you entered : 99.987654
Advertisement
#include<stdio.h>
int main()
{
char ch;
int i;
float f;
double d;
printf("Reading multiple values using a single scanf() function - ");
printf("\n");
printf("Enter the values separated by space/tab/newline : ");
scanf("%c %d %f %lf %ld",&ch, &i, &f, &d, &l);
printf("Display multiple using single printf() function -");
printf("\n");
printf("the values you've entered are : %c %d %f %lf %ld", ch, i, f, d, ld);
return 0;
}
Enter the values separated by space/tab/newline : a 10 2.5 9.98765 1234567
Display multiple using single printf() function -
the values you've entered are : a 10 2.500000 9.987650 1234567
#include<stdio.h>
int main()
{
char ch;
int i;
float f;
double d;
long l;
/* Reading values using scanf() */
printf("Reading the values -");
printf("\n");
printf("Enter a char value : ");
scanf("%c",&ch); /* Reading a char value with format specifier %c */
printf("Enter an int value : ");
scanf("%d",&i); /* Reading an int value with format specifier %d */
printf("Enter a long value : ");
scanf("%ld",&l); /* Reading an long value with format specifier %ld */
printf("Enter a float value : ");
scanf("%f",&f); /* Reading an float value with format specifier %f */
printf("Enter a double value : ");
scanf("%lf",&d); /* Reading an double value with format specifier %lf */
/* Displaying values using printf() */
printf("Display the values you've entered -");
printf("\n");
/* Displaying a char value with format specifier %c and width of 10 characters */
printf("The char value you entered : %10c", ch);
printf("\n");
/* Displaying an int value with format specifier %d */
printf("The int value you entered : %-7d int", i);
printf("\n");
/* Displaying a long value with format specifier %ld */
printf("The long value you entered : %5ld", l);
printf("\n");
/* Displaying a float value with format specifier %f */
printf("The float value you entered : %10f", f);
printf("\n");
/* Displaying a double value with format specifier %lf */
printf("The double value you entered : %2lf", d);
return 0;
}
Reading the values -
Enter a char value : a
Enter an int value : 1234
Enter a long value : 12345678
Enter a float value : 2.3
Enter a double value : 999.98765
Display the values you've entered -
The char value you entered : a
The int value you entered : 1234 int
The long value you entered : 12345678
The float value you entered : 2.300000
The double value you entered : 999.987650
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement