Input and Output

0 margaritabaird7 November 13, 2020

In this tutorial, We will talk about the input and output operations in C Programming. In C Programming, We use scanfto take input and printfto display output.

1. scanf

The scanfis used to take input from standard input. The syntax of scanf is:

scanf("%X", &Variable_Name);

where %X is the format specifier that tells the compiler what type of data is in a variable and & is the address operator which tells the compiler to change the real value of the variable. 

2. printf

The printf is used to display output on the screen. The syntax of printf is

printf("%X", variable_name);

where %X is the format specifier that tells the compiler what type of data is in a variable.

Let’s look at one example using scanf and printf.

#include <stdio.h> 
  
int main() 
{ 
  
    // Declare the variables 
    int num; 
    char ch; 
    float f; 
  
    // --- Integer --- 
  
    // take input from user
    printf("Enter the integer number : "); 
    scanf("%d", &num); 
  
    // print the output 
    printf("\nEntered integer value is : %d", num); 
  
    // --- Float --- 
  
    // take input from user
    printf("\n\nEnter the float number : "); 
    scanf("%f", &f); 
  
    // print the output 
    printf("\nEntered Float Value is : %f", f);

    // --- Character --- 
  
    // take input from user
    printf("\n\nEnter the character : "); 
    scanf("%c", &ch); 
  
    // print the output 
    printf("\nEntered character is : %c", ch); 
  
    return 0; 
}

The output of the above program is:

Enter the integer number : 12

Entered integer value is : 12

Enter the float number : 12.5

Entered Float Value is : 12.500000

Enter the character : C
Entered character is : C