Home Tutorials C Tutorial Input and Output
Input and Output

Input and Output


To interact with users, C uses standard library functions: printf() to display information (Output) and scanf() to receive information (Input).

Practical Example: Reading User Input

#include <stdio.h>

int main() {
    int number;

    // Asking the user for input
    printf("Enter a number: ");

    // Reading and storing the input
    scanf("%d", &number);

    // Displaying the stored value
    printf("You entered: %d\n", number);

    return 0;
}

Deep Dive: How it Works

  • scanf(): This function pauses the program and waits for the user to type something on the keyboard.
  • The Ampersand (&number): This is the address-of operator. It tells C the exact location in the computer's memory where the user's input should be saved.
  • Format Specifiers: These act as placeholders that tell the compiler what type of data to expect.
Specifier Data Type
%d Integers (Whole numbers)
%f Float (Decimal numbers)
%c Char (Single characters)
Example

🏋️ Test Yourself With Exercises

Take our quiz on Input and Output to test your knowledge.

Browse Quizzes »