Advertisement



< Prev
Next >



C- scanf() and printf() function




In our last article, we introduced you to the concept of formatted console input/output functions. In this article, we are going to explain two of the most commonly used formatted console input/output functions.


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.



Both scanf() and printf() function use specific format specifiers to display specific values. Let's look at the code that allows us to read and display a single value at time using a single scanf() and printf() function.





Let's look at the code that allows us to read and display multiple values at time using a single scanf() and printf() function.

#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;
}


Output


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





  • Displaying values with specific width in printf()


  • We can even optionally do the left and right justification of the value to be printed and have them printed in a specific width using printf() function. For those who have not read our previous article explaining the concept of specifying width of the value to be printed within format specifier and to perform its left and right justification, please refer to optional specifier within a format specifier.

    #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;
    }
    


    Output


    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


    As you may see in the code and its output, we have used multiple format specifiers and have also specified a specific width of each value to be displayed on the screen, within the printf() function.




    Please share this article -





    < Prev
    Next >
    < Formatted Console I/O functions
    sscanf() and sprintf() function >



    Advertisement

    Please Subscribe

    Please subscribe to our social media channels for daily updates.


    Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




    Advertisement



    Notifications



    Please check our latest addition

    C#, PYTHON and DJANGO


    Advertisement