LERAN C PROGRAMMING

Learn one of the most powerful programming languages in the world and become a rockstar developer.

C Input Output

In this tutorial, you will learn to use scanf() function to take input from the user, and printf() function to display output to the user.

C Output

In C programming, printf() is one of the main output function. The function sends formatted output to the screen. For example,

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

Output :

Hello World

C Input

In C programming, scanf() is one of the commonly used function to take input from the user. The scanf() function reads formatted input from the standard input such as keyboards.

#include <stdio.h>
 int main()
 {
     int testInteger;
      printf("Enter an integer: ");
      scanf("%d", &testInteger); 
      printf("Number = %d",testInteger);
      return 0;
 }

Output :

Enter integer: 99
Number = 99

Here, we have used %d format specifier inside the scanf() function to take int input from the user. When the user enters an integer, it is stored in the testInteger variable.

Notice, that we have used &testInteger inside scanf(). It is because &testInteger gets the address of testInteger, and the value entered by the user is stored in that address.

C Input Multiple Values

Here's how you can take multiple inputs from the user and display them.

#include<stdio.h>

int main()
{
     printf("\n\n\t\Adzetech - Best place to learn program\n\n\n");

     int num1, num2;

     printf("Enter two numbers number\n");
 
    // Taking integer as input from user
    scanf("%d%i", &num1, &num2);
    printf("\n\nThe two numbers You have entered are %d and %i\n\n", num1, num2);

    printf("\n\n\t\t\tCoding is Fun !\n");
    printf("\n\t\tPowered by Suresh Chand !\n");
 
    return 0;
}
Output :

Enter two numbers number : 1 2
The two numbers You have entered are 1 and 2


Previous Next
x
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Privacy Policy That's Fine