CS Engineering Gyan

Input and Output in C

A program that only works with fixed, hardcoded values is rarely useful in the real world. Most software needs to communicate with the people using it, whether that means displaying results on a screen or accepting information typed in by a user. This two-way communication is handled through input and output operations, and in C, this is primarily done using two functions that you will use constantly throughout your programming journey.

Although printf and scanf look simple on the surface, they hide a surprising amount of detail once you start working with different data types, formatting requirements, and edge cases involving user input. Taking the time to understand these functions properly now will save you from confusing bugs later, especially once your programs start handling more complex combinations of input.

In this tutorial, you will learn how to display output using printf, how to accept user input using scanf, what format specifiers and escape sequences are, how to work with individual characters using getchar and putchar, and some of the most common mistakes beginners run into when working with input and output in C.


Displaying Output with printf

The printf function is used to display text and values on the screen. It is one of the very first functions every C programmer learns, and it remains one of the most frequently used throughout even advanced programs.

Example

#include <stdio.h>

int main() {

    printf("Welcome to CS Engineering Gyan!");

    return 0;

}

Output

Welcome to CS Engineering Gyan!

When printf is given plain text without any format specifiers, it simply displays that text exactly as written. Things become more interesting once variables are introduced into the output using format specifiers.


Format Specifiers

Format specifiers are special placeholders inside a printf or scanf statement that tell the compiler what type of data is being displayed or read. Each data type in C has its own corresponding specifier.

Specifier Used For
%d Displaying or reading an integer value.
%f Displaying or reading a floating-point value.
%c Displaying or reading a single character.
%s Displaying or reading a string of characters.
%lf Reading a double value using scanf, since %f alone is used for double values only in printf.

Example

#include <stdio.h>

int main() {

    int episode = 25;

    float rating = 4.7;

    char grade = 'A';

    printf("Episode %d rated %.1f, grade %c", episode, rating, grade);

    return 0;

}

Output

Episode 25 rated 4.7, grade A

Notice how each format specifier lines up with a corresponding variable listed after the format string, in the same order they appear.


Accepting Input with scanf

The scanf function allows a program to read values typed in by the user during execution, making programs interactive rather than limited to fixed, predetermined data.

Example

#include <stdio.h>

int main() {

    int age;

    printf("Enter your age: ");

    scanf("%d", &age);

    printf("You entered: %d", age);

    return 0;

}

Output

Enter your age: 21

You entered: 21

Notice the ampersand symbol placed before the variable name inside scanf. This symbol represents the address-of operator, and it tells scanf exactly where in memory the entered value should be stored.


Reading Multiple Values at Once

scanf is not limited to reading a single value at a time. Multiple format specifiers can be combined within a single scanf call to accept several pieces of input together.

Example

#include <stdio.h>

int main() {

    int day, month, year;

    printf("Enter date as day month year: ");

    scanf("%d %d %d", &day, &month, &year);

    printf("Date entered: %d/%d/%d", day, month, year);

    return 0;

}

Output

Enter date as day month year: 15 8 2026

Date entered: 15/8/2026

When multiple values are separated by spaces on the same line, scanf automatically matches each typed value to the corresponding format specifier and variable in order.


Escape Sequences

Escape sequences are special character combinations that represent characters which cannot be typed directly into a string, such as a new line or a tab space. They always begin with a backslash.

Escape Sequence Meaning
\n Moves the cursor to the beginning of the next line.
\t Inserts a horizontal tab space.
\\ Displays a single backslash character.
\" Displays a double quote character within a string.

Example

#include <stdio.h>

int main() {

    printf("CS Engineering Gyan\nSubscribe for more tutorials!");

    return 0;

}

Output

CS Engineering Gyan

Subscribe for more tutorials!

Character Input and Output

Besides printf and scanf, C also provides dedicated functions for working with single characters, which can be useful for simple, character-based input and output tasks.

Function Purpose
getchar Reads a single character typed by the user.
putchar Displays a single character on the screen.

Example

#include <stdio.h>

int main() {

    char letter;

    printf("Enter a grade letter: ");

    letter = getchar();

    printf("You entered: ");

    putchar(letter);

    return 0;

}

Output

Enter a grade letter: A

You entered: A

Reading Strings with scanf

Reading a full word or sentence requires a slightly different approach compared to reading numbers or single characters, since strings in C are stored as arrays of characters.

Example

#include <stdio.h>

int main() {

    char channelName[30];

    printf("Enter a channel name: ");

    scanf("%s", channelName);

    printf("Channel entered: %s", channelName);

    return 0;

}

Output

Enter a channel name: CSEngineeringGyan

Channel entered: CSEngineeringGyan

Unlike variables of other types, arrays do not require an ampersand before their name in scanf, since the array name itself already represents the memory address where the data will be stored. It is also worth noting that %s stops reading at the first space, which means it cannot capture multi-word input directly.


Controlling Output Width and Precision

printf allows fine control over how numbers are displayed, including how many decimal places to show or how much space a value should occupy, which is especially useful when formatting output into neat, aligned columns.

Example

#include <stdio.h>

int main() {

    float averageWatchTime = 6.856;

    printf("Average watch time: %.2f minutes", averageWatchTime);

    return 0;

}

Output

Average watch time: 6.86 minutes

The number placed between the percent sign and the letter f controls how many digits appear after the decimal point, rounding the value automatically if necessary.


Best Practices for Input and Output


Common Mistakes Beginners Make

Mistake Correct Practice
Forgetting the ampersand before a variable name in scanf. Always include the address-of operator when reading into a simple variable using scanf.
Using %f to read a double value with scanf. Use %lf specifically when reading double values with scanf, even though %f is used for both in printf.
Expecting %s to capture input containing spaces. Understand that %s stops at the first space, and consider alternative approaches when full sentences are needed.
Mismatching the number of format specifiers with the number of variables provided. Ensure the number and order of format specifiers exactly matches the variables listed in printf or scanf.

Frequently Asked Interview Questions

  1. What is the purpose of the printf function in C?
    The printf function is used to display text and formatted values on the screen.
  2. What is the purpose of the scanf function in C?
    The scanf function is used to read input values typed by the user during program execution.
  3. What does the ampersand symbol represent in a scanf statement?
    The ampersand represents the address-of operator, which tells scanf where in memory to store the entered value.
  4. Why is the ampersand not required when reading a string into a character array using scanf?
    An array name already represents the memory address of its first element, so scanf does not need an additional address-of operator in that case.
  5. What is a format specifier in C?
    A format specifier is a placeholder within printf or scanf that indicates the data type of the value being displayed or read.
  6. What is the difference between %f and %lf in scanf?
    While %f is used for float values, %lf must be used specifically when reading double values with scanf, even though printf treats both the same way.
  7. What is an escape sequence in C?
    An escape sequence is a special character combination beginning with a backslash that represents characters such as a new line or tab that cannot be typed directly into a string.
  8. What do the getchar and putchar functions do?
    getchar reads a single character entered by the user, while putchar displays a single character on the screen.
  9. Why does %s stop reading input at a space?
    The %s specifier is designed to read a single word, and it automatically stops at the first whitespace character it encounters.
  10. How can decimal precision be controlled when displaying a floating-point value?
    A number placed between the percent sign and the letter f in printf specifies how many digits should appear after the decimal point.
  11. What happens if the number of format specifiers does not match the number of variables in printf?
    This mismatch can lead to incorrect or unpredictable output, since each specifier is expected to correspond to exactly one variable in order.

Summary

Input and output operations are what allow a C program to interact meaningfully with the people using it, transforming static code into something genuinely interactive. By mastering printf for displaying formatted output and scanf for accepting user input, along with supporting tools like escape sequences and character-based functions, you gain the ability to build programs that respond dynamically to real user data.

In this tutorial, you learned how to display values using printf, accept single and multiple inputs using scanf, work with escape sequences and format specifiers, handle individual characters, and control the precision of floating-point output. With these fundamentals in place, you are ready to move on to conditional statements, which allow your programs to make decisions based on the values they read or calculate.


← Previous: Operators in C Next: Conditional Statements →

Home Visit Our YouTube Channel