Skip to content
C Programming – Input & Character I/O

C Programming: Input & Character I/O

1. Input in C

Input is the activity of supplying data to a C program from the user at run time.

In C, the scanf() function is commonly used to accept formatted input from the keyboard.

Example: If a program asks the user to enter their age, the value entered by the user is called input.

Syntax of scanf()

scanf("%format_specifier", &variable);

The & symbol is called the address-of operator. It provides the memory address of the variable to scanf().

Common Format Specifiers

Data Type Format Specifier Example
int %d scanf("%d", &x);
float %f scanf("%f", &x);
double %lf scanf("%lf", &x);
char %c scanf("%c", &ch);

2. Basic scanf() Example

#include <stdio.h>

int main()
{
    int x;

    printf("Enter any number: ");
    scanf("%d", &x);

    printf("Your number is %d", x);

    return 0;
}

Sample Output

Enter any number: 25
Your number is 25

3. Important Points Regarding scanf()

  1. When there is more than one format specifier in scanf(), the input values can generally be separated using spaces or the Enter key.
  2. printf() does not require a \n merely because it follows a scanf(). A newline is needed only when you want the cursor to move to the next line.
  3. Special care is required when reading a char immediately after numeric input because the Enter key leaves a newline character (\n) in the input buffer.
Important: A common problem occurs when scanf("%c", &ch) is used immediately after reading an integer with scanf("%d", &x). The %c may read the leftover newline character.

4. Reading Character After Numeric Input

Consider the following program:

#include <stdio.h>

int main()
{
    int x;
    char ch;

    printf("Enter any number: ");
    scanf("%d", &x);

    printf("Enter any character: ");
    scanf(" %c", &ch);

    printf("x = %d\n", x);
    printf("ch = %c", ch);

    return 0;
}
Why is there a space before %c?

The space in " %c" tells scanf() to skip whitespace characters such as spaces, tabs, and newlines before reading the character.

5. Character I/O Functions

A character variable stores a single character. In C, a char typically occupies 1 byte of memory.

The %c format specifier is used with formatted I/O functions to read or display character data.

Example: Displaying a Character

#include <stdio.h>

int main()
{
    char ch;

    ch = '$';

    printf("ch = %c", ch);

    return 0;
}
ch = $

Example: Reading a Character

#include <stdio.h>

int main()
{
    char ch;

    printf("Enter any character: ");
    scanf("%c", &ch);

    printf("Your character is = %c", ch);

    return 0;
}

6. Character Input Example: Menu

Character input can also be used to accept a user’s menu choice.

#include <stdio.h>

int main()
{
    char ch;

    printf("Menu");
    printf("\n1. Add");
    printf("\n2. Modify");
    printf("\n3. Delete");
    printf("\n4. Exit");

    printf("\nEnter your choice: ");
    scanf(" %c", &ch);

    printf("Your choice is %c", ch);

    return 0;
}
Tip: Using scanf(" %c", &ch) is useful when you want to ignore leftover whitespace before reading a character.

7. Formatted and Unformatted I/O Functions

The standard I/O functions of C are declared in the stdio.h header file.

Type Description Examples
Formatted I/O Uses format specifiers to control how data is read or displayed. printf(), scanf(), fprintf(), sprintf()
Character / Unformatted I/O Character-oriented functions that do not use format specifiers. getchar(), putchar()
Note: The term “unformatted I/O” is commonly used for functions such as getchar() and putchar(). These functions operate directly on characters and do not require format specifiers.

8. putchar()

putchar() is used to display a single character on the screen.

Syntax

putchar(character);

Example

#include <stdio.h>

int main()
{
    char ch = 'A';

    putchar(ch);

    return 0;
}
A

9. getchar()

getchar() reads one character from standard input and returns that character.

Syntax

variable = getchar();

Example

#include <stdio.h>

int main()
{
    char ch;

    printf("Enter any character: ");
    ch = getchar();

    printf("Your character is: ");
    putchar(ch);

    return 0;
}

How the Program Works

  1. A character variable ch is declared.
  2. getchar() waits for character input.
  3. The entered character is stored in ch.
  4. putchar() displays the character.

10. getch() and getche()

getch() and getche() are commonly associated with the non-standard conio.h library.

Important: getch(), getche(), and putch() are not part of standard C. They are available in some compilers and environments, but may not work in GCC, Clang, or other standards-compliant environments.
Function Purpose Echoes Character? Requires Enter?
getch() Reads one character immediately No No
getche() Reads one character immediately Yes No
getchar() Reads a character from standard input Yes Normally yes when entered interactively

11. Example Using getch()

#include <stdio.h>
#include <conio.h>

int main()
{
    char ch;

    printf("Enter any character: ");

    ch = getch();

    printf("\nYour character is: ");
    putch(ch);

    return 0;
}

If the user enters A, the character is accepted immediately without displaying it during input. The program then displays it using putch().

Using getche()

Replace:

ch = getch();

with:

ch = getche();

Now observe the difference. The entered character will be displayed immediately.

12. Quick Comparison

Function Header Purpose Format Specifier?
printf() stdio.h Formatted output Yes
scanf() stdio.h Formatted input Yes
putchar() stdio.h Output one character No
getchar() stdio.h Input one character No
getch() conio.h* Read one character without waiting for Enter No
getche() conio.h* Read one character and echo it No

* conio.h functions are compiler/platform dependent and are not standard C.

13. Practice Programs

Practice 1

Sum of Two Integers

Write a C program to create two integer variables, accept user input into them, and display their sum.

Hint:

sum = x + y;
Practice 2

Average of Three Numbers

Write a C program to accept any three numbers from the user and display their average.

Hint:

avg = (a + b + c) / 3.0;

Using 3.0 instead of 3 helps obtain a floating-point result.

Practice 3

Simple Interest

Write a C program to calculate and print simple interest using the principal, rate of interest, and tenure entered by the user.

Formula:

SI = (P * N * R) / 100;
  • P = Principal amount
  • N = Number of years / tenure
  • R = Rate of interest
Practice 4

Area of a Circle

Write a C program to calculate the area of a circle using a radius entered by the user.

Formula:

area = pi * r * r;
  • pi is a float variable initialized to 3.14.
  • r is an integer variable entered by the user.
  • area is a float variable.
Practice 5

All Basic Data Types

Write a C program to create variables representing the basic data types, accept user input for them, and display their values.

For example, use:

  • int
  • float
  • double
  • char

14. Additional Practice Questions

Question 6: Write a C program to accept the user’s age and display it.
Question 7: Write a C program to accept two floating-point numbers and display their sum, difference, product, and quotient.
Question 8: Write a C program to accept a character and display the character using both printf() and putchar().
Question 9: Write a C program to accept a character using getchar() and display it using putchar().
Question 10: Write a C program that displays the following menu and accepts the user’s choice as a character:
1. Add
2. Modify
3. Delete
4. Exit
Question 11: Write a C program to accept the length and breadth of a rectangle and calculate its area and perimeter.
Question 12: Write a C program to accept temperature in Celsius and convert it into Fahrenheit.
F = (C * 9 / 5) + 32
Question 13: Write a C program to accept three integers and display their sum, average, largest value, and smallest value.
Question 14: Write a C program to accept a character and display its ASCII value.
Question 15: Write a C program to accept a character using getchar() and determine whether the entered character is a digit, alphabet, or special character.

15. Key Points to Remember

  • scanf() is commonly used for formatted input.
  • printf() is commonly used for formatted output.
  • %d is used for int.
  • %f is used for float.
  • %lf is used for double input.
  • %c is used for character input/output.
  • Use & with scanf() for ordinary variables because scanf() needs their addresses.
  • Use scanf(" %c", &ch) when appropriate to skip whitespace before reading a character.
  • getchar() reads one character from standard input.
  • putchar() displays one character.
  • getch() and getche() are non-standard/compiler-dependent functions.
  • Always use int main() and return 0; for standard C programs.
Learning Tip: Practice each program by first writing the variables, then writing the input statements, followed by the calculation, and finally the output statement. This makes it easier to understand the flow of a C program.