0d: 00h: 00m: 00s

🎁 Get 3 extra months of learning — completely FREE

That's 90 extra days to master new skills, build more projects, and land your dream job.

Become a PRO
Background Image

🎁 Get 3 extra months of learning completely FREE

Become a PRO
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.