SrcForge

Lesson 9

Basic Input and Output (I/O)

Input and Output (I/O) are the fundamental operations performed in every C program. Input refers to the data provided by the user, while output refers to the information displayed by the program, primarily using the printf() and scanf() functions from stdio.h.

C Track

Save progress as you learn.

44 lessons

Lesson content

Basic Input and Output (I/O) in C Programming

Input and Output (I/O) are the fundamental operations performed in every C program. Input refers to the data provided by the user, while output refers to the information displayed by the program.

In C programming, input and output operations are primarily performed using the scanf() and printf() functions, which are available in the stdio.h (Standard Input/Output) header file.

Understanding basic I/O operations is essential because almost every C program interacts with users by accepting input and displaying results. Whether you're building a calculator, student management system, or banking application, input and output functions form the foundation of user interaction.

Prerequisites

  • Introduction to C Programming
  • Structure of a C Program
  • Data Types in C Programming
  • Variables and Constants
  • Tokens in C Programming

Core Concepts: Basic Input and Output in C

Input and output operations allow a program to communicate with users. Input refers to data entered by the user, while output refers to information displayed by the program.

The stdio.h header file provides the standard functions required for input and output operations.

#include <stdio.h>

Output Function: printf()

The printf() function is used to display output on the screen.

Syntax

printf("format string", variables);
  • Format string specifies what should be displayed.
  • Variables are optional and represent the values to be printed.

Example

printf("Hello, World!");

Output: Hello, World!

Input Function: scanf()

The scanf() function is used to read input from the keyboard.

Syntax

scanf("format_specifier", &variable);
  • Format specifier indicates the type of data to read.
  • & (address-of operator) provides the memory address where the input value will be stored.

Example

scanf("%d", &age);

Common Format Specifiers

Format SpecifierData TypeDescription
%dintInteger
%ffloatFloating-point number
%lfdoubleDouble precision number
%ccharSingle character
%sCharacter arrayString
%uunsigned intUnsigned integer
%ldlong intLong integer
%xIntegerHexadecimal value

Basic Input and Output Programs

Program 1: Print "Hello, World!"

This is the first program most beginners write in C.

#include <stdio.h>                          // Includes the standard input/output library

int main()                                  // Main function
{
    printf("Hello, World!");                // Displays a message on the screen

    return 0;                               // Ends the program
}

Output: Hello, World!

Program 2: Display a Variable

This program prints the value stored in a variable.

#include <stdio.h>                          // Includes the standard input/output library

int main()                                  // Main function
{
    int age = 20;                           // Declares and initializes an integer variable

    printf("Age = %d", age);                // Displays the value of age

    return 0;                               // Ends the program
}

Output: Age = 20

Program 3: Read an Integer from the User

This program accepts an integer from the user and displays it.

#include <stdio.h>                              // Includes the standard input/output library

int main()                                      // Main function
{
    int number;                                 // Declares an integer variable

    printf("Enter a number: ");                 // Prompts the user

    scanf("%d", &number);                       // Reads an integer from the keyboard

    printf("You entered: %d", number);          // Displays the entered number

    return 0;                                   // Ends the program
}

Sample Output: Enter a number: 25 / You entered: 25

Program 4: Read Two Numbers and Display Their Sum

This program demonstrates both input and output operations.

#include <stdio.h>                              // Includes the standard input/output library

int main()                                      // Main function
{
    int num1, num2, sum;                        // Declares integer variables

    printf("Enter two numbers: ");              // Prompts the user

    scanf("%d %d", &num1, &num2);               // Reads two integers

    sum = num1 + num2;                          // Calculates the sum

    printf("Sum = %d", sum);                    // Displays the result

    return 0;                                   // Ends the program
}

Sample Output: Enter two numbers: 15 20 / Sum = 35

Program 5: Read Different Data Types

This program accepts multiple types of input from the user.

#include <stdio.h>                                  // Includes the standard input/output library

int main()                                          // Main function
{
    int age;                                        // Integer variable

    float salary;                                   // Float variable

    char grade;                                     // Character variable

    printf("Enter age, salary, and grade: ");       // Prompts the user

    scanf("%d %f %c", &age, &salary, &grade);       // Reads input values

    printf("Age = %d
", age);                      // Displays age

    printf("Salary = %.2f
", salary);              // Displays salary

    printf("Grade = %c", grade);                    // Displays grade

    return 0;                                       // Ends the program
}

Difference Between printf() and scanf()

Featureprintf()scanf()
PurposeDisplays outputAccepts input
DirectionProgram → UserUser → Program
Header Filestdio.hstdio.h
Uses Format SpecifiersYesYes
Uses & OperatorNoYes (except for strings)

Common Mistakes and Pitfalls

Incorrect CodeCorrect CodeReason
scanf("%d", number);scanf("%d", &number);Missing address operator (&).
printf("%d", &num);printf("%d", num);printf() requires the value, not the address.
scanf("%f", &num); where num is intscanf("%d", &num);Incorrect format specifier.
printf("%d", value); where value is floatprintf("%f", value);Wrong format specifier.

Best Practices

  • Always include the stdio.h header file when using printf() and scanf().
  • Use the correct format specifier for each data type.
  • Always use the & operator with scanf() for variables (except strings).
  • Display meaningful prompts before accepting input.
  • Format output neatly using \n and appropriate precision for floating-point numbers.
  • Validate user input in larger applications to avoid unexpected behavior.

When NOT to Use scanf()

Avoid using scanf() when:

  • Reading an entire line of text that may contain spaces.
  • Processing complex formatted input.
  • Building secure applications where input validation is critical.

In such cases, functions like fgets() are generally preferred.

Summary / Key Takeaways

  • Input allows users to provide data to a program.
  • Output displays information to the user.
  • printf() is used to display output.
  • scanf() is used to read user input.
  • Both functions are defined in the stdio.h header file.
  • Format specifiers determine the type of data to read or display.
  • Always use the correct format specifier and the address operator (&) with scanf().

FAQ About Basic Input and Output in C

1. What is input and output in C programming?

Input is the data entered by the user, while output is the information displayed by the program.

2. Which header file is required for printf() and scanf()?

Both functions are available in the stdio.h (Standard Input/Output) header file.

3. Why is the & operator used with scanf()?

The & operator provides the memory address of a variable, allowing scanf() to store the user-entered value correctly.

4. What is a format specifier in C?

A format specifier is a placeholder (such as %d, %f, or %c) that tells printf() and scanf() what type of data to display or read.

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

printf() is used to display output on the screen, whereas scanf() is used to accept input from the user.

6. Can scanf() read multiple values at once?

Yes. By using multiple format specifiers, scanf() can read several values in a single statement, such as scanf("%d %f %c", &number, &price, &grade);

Prev: TokensBack to hub