Input and Output in C

The printf() and scanf() functions are used for input and output in the C language. In this tutorial we will learn how to use the scanf() function to take user input and the printf() function to display output to the user.


Input refers to the act of feeding data into a software. A file or the command line can be used to provide input. When we mention Output, we want to display some information on the screen, printer, or in any record. C programming provides a plethora of inherent capacities for displaying information on the PC screen as well as saving it in text or linked documents.

1. printf( ) Function in C Language:

printf() is one of the main output functions in C programming. This function displays output on the console.


Example 1: String Output:

C
#include <stdio.h>
int main()
{
    // Displays the string inside quote marks.
    printf("Hello HashCoded");
    return 0;
}

Example 2: Integer Output:

C
#include <stdio.h>
int main()
{
    int number = 7;
    printf("Number = %d", number);
    return 0;
}

For printing int data types, we use the %d format specifier. The value of number will be used in place of the %d inside the quotations in this case.


Example 3: float and double Output:

C
#include <stdio.h>
int main()
{
  float decimal1 = 12.2;
  double decimal2 = 22.29;

  printf("decimal1 = %f\n", decimal1);
  printf("decimal2 = %lf", decimal2);

  return 0;
}

We can use %f format specifier to print float. To print double values, we use %lf.


Example 4: Character Output:

C
#include <stdio.h>
int main()
{
  char character = 'H';
  printf("character = %c", character);

  return 0;
}

To print char, we use %c format specifier.




2. scanf() Function in C Language:

scanf() is a commonly used function in C programming to accept user input. It reads the input data from the console.


Example 5: Integer Input and Output

C
#include <stdio.h>
int main()
{
    int number;
    printf("Enter a number: ");
    scanf("%d", &number);  //Taking input from the user
    printf("Number = %d",number);
    return 0;
}

In order to accept int input from the user, we have to included %d format specifier inside the *scanf() function. When the user enters an number, it is stored in the number variable.


Example 6: Float and Double Input and Output

C
#include <stdio.h>
int main()
{
    float number1;
    double number2;

    printf("Enter a number: ");
    scanf("%f", &number1);

    printf("Enter another number: ");
    scanf("%lf", &number2);

    printf("number1 = %f \n", number1);
    printf("number2 = %lf", number2);

    return 0;
}

We use %f and %lf format specifier for float and double respectively.


Example 7: Character Input/Output

C
#include <stdio.h>
int main()
{
    char chracter;

    printf("Enter a character: ");
    scanf("%c",&chracter);

    printf("You entered character: %c.", chracter);
    return 0;
}

When a user enters a character into the above program, the character is not stored. An integer value (ASCII value) is being stored. When we display that value using %c text format, the entered character is displayed. If we use %d to display the character, it's ASCII value is printed.


Example 8: ASCII value

C
#include <stdio.h>
int main()
{
    char chracter;

    printf("Enter a character: ");
    scanf("%c",&chracter);
    //%c is used to print character.
    printf("You entered character: %c.", chracter);
    //%d is used to print ASCII value
    printf("ASCII value is %d.", chracter);
    return 0;
}