Ends in 7 days

BLACK FRIDAY: Receive 83% off Programiz PRO

+🎁 Buy 1 year, gift 1 year FREE — Split with a friend for just $49.50 each

Start FREE trial →
Background Image

Buy 1 year, Gift 1 year —completely FREE

Start FREE trial →
Kelvin Mungai
last year
Kelvincountry asked

What is the difference between scanf() and printf()?

Abhilekh Gautam
Expert
last year

In C, scanf() is used to read input from the user, while printf() is used to display output on the screen.

Think of it this way:

  • scanf() receives data (input)

  • printf() sends data to the screen (output)

Example:

#include 

int main() {
    int age;

    printf("Enter your age: ");    // Output
    scanf("%d", &age);    // Input

    printf("You are %d years old.", age);    // Output
    return 0;
}

Here, printf() first prompts the user, and then scanf() captures the input into the age variable. After that, printf() is used again to show the result.

C
This question was asked as part of the Learn C Programming course.