Kelvin Mungai
4 months ago
Kelvincountry asked

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

Abhilekh Gautam
Expert
2 months ago

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.