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.
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
Your number is 25
3. Important Points Regarding scanf()
-
When there is more than one format specifier in
scanf(), the input values can generally be separated using spaces or the Enter key. -
printf()does not require a\nmerely because it follows ascanf(). A newline is needed only when you want the cursor to move to the next line. -
Special care is required when reading a
charimmediately after numeric input because the Enter key leaves a newline character (\n) in the input buffer.
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;
}
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;
}
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;
}
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()
|
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;
}
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
- A character variable
chis declared. getchar()waits for character input.- The entered character is stored in
ch. putchar()displays the character.
10. getch() and getche()
getch() and getche() are commonly associated
with the non-standard conio.h library.
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
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;
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.
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
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;
piis a float variable initialized to3.14.ris an integer variable entered by the user.areais a float variable.
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:
intfloatdoublechar
14. Additional Practice Questions
printf() and putchar().
getchar()
and display it using putchar().
1. Add
2. Modify
3. Delete
4. Exit
F = (C * 9 / 5) + 32
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.%dis used forint.%fis used forfloat.%lfis used fordoubleinput.%cis used for character input/output.-
Use
&withscanf()for ordinary variables becausescanf()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()andgetche()are non-standard/compiler-dependent functions. -
Always use
int main()andreturn 0;for standard C programs.