Mastering C Input and Output (I/O): scanf() and printf() Explained
C Input and Output (I/O) in C
This tutorial walks you through using scanf() to capture user input and printf() to present output, with clear examples and best practices.
Video: Getting User Input in C Programming
Watch the accompanying video to see live demonstrations of scanf() in action.
C Output
The printf() function is the primary tool for sending formatted text to the console. Below are illustrative examples.
Example 1: Basic Text Output
#include <stdio.h>
int main()
{
printf("C Programming");
return 0;
}
Output
C Programming
This program prints a simple string. Key points:
- The
main()function is mandatory; program execution starts here. - Include
stdio.hto accessprintf(). - The optional
return 0;signals successful completion.
Example 2: Integer Output
#include <stdio.h>
int main()
{
int testInteger = 5;
printf("Number = %d", testInteger);
return 0;
}
Output
Number = 5
The %d specifier injects the integer value into the output string.
Example 3: Floating‑Point Output
#include <stdio.h>
int main()
{
float number1 = 13.5;
double number2 = 12.4;
printf("number1 = %f\n", number1);
printf("number2 = %lf", number2);
return 0;
}
Output
number1 = 13.500000 number2 = 12.400000
Use %f for float and %lf for double values.
Example 4: Character Output
#include <stdio.h>
int main()
{
char chr = 'a';
printf("character = %c", chr);
return 0;
}
Output
character = a
Print a single character with %c.
C Input
The scanf() function reads formatted data from standard input, typically the keyboard. Below are practical examples.
Example 5: Integer Input/Output
#include <stdio.h>
int main()
{
int testInteger;
printf("Enter an integer: ");
scanf("%d", &testInteger);
printf("Number = %d", testInteger);
return 0;
}
Output
Enter an integer: 4 Number = 4
The address operator & supplies scanf() with the memory location to store the user’s input.
Example 6: Float and Double Input/Output
#include <stdio.h>
int main()
{
float num1;
double num2;
printf("Enter a number: ");
scanf("%f", &num1);
printf("Enter another number: ");
scanf("%lf", &num2);
printf("num1 = %f\n", num1);
printf("num2 = %lf", num2);
return 0;
}
Output
Enter a number: 12.523 Enter another number: 10.2 num1 = 12.523000 num2 = 10.200000
Example 7: Character Input/Output
#include <stdio.h>
int main()
{
char chr;
printf("Enter a character: ");
scanf("%c", &chr);
printf("You entered %c.", chr);
return 0;
}
Output
Enter a character: g You entered g.
A character is internally represented by its ASCII integer value; printing it with %c restores the original symbol.
Example 8: Displaying ASCII Value
#include <stdio.h>
int main()
{
char chr;
printf("Enter a character: ");
scanf("%c", &chr);
printf("You entered %c.\n", chr);
printf("ASCII value is %d.", chr);
return 0;
}
Output
Enter a character: g You entered g. ASCII value is 103.
Handling Multiple Inputs
Collecting several values in a single scanf() call is efficient and concise.
#include <stdio.h>
int main()
{
int a;
float b;
printf("Enter an integer and then a float: ");
scanf("%d%f", &a, &b);
printf("You entered %d and %f", a, b);
return 0;
}
Output
Enter integer and then a float: -3 3.4 You entered -3 and 3.400000
Format Specifiers for I/O
Below is a concise reference for common C data types and their printf() / scanf() specifiers.
%d–int%f–float%lf–double%c–char
| Data Type | Format Specifier |
|---|---|
int | %d |
char | %c |
float | %f |
double | %lf |
short int | %hd |
unsigned int | %u |
long int | %li |
long long int | %lli |
unsigned long int | %lu |
unsigned long long int | %llu |
signed char | %c |
unsigned char | %c |
long double | %Lf |
C Language
- Understanding the Buffer Gate: Amplification and Signal Integrity in Digital Circuits
- C# Fundamentals: Input and Output Essentials
- Mastering C++ Input and Output: A Practical Guide
- Python File I/O: Mastering File Operations, Reading, Writing, and Management
- Mastering D Latches: Design, Operation, and Key Differences
- Mastering Java I/O: Files, Streams, and Input/Output Fundamentals
- Mastering Input and Output in C Programming
- Mastering File I/O in C: Creating, Opening, and Managing Files
- Mastering C# File I/O: Reading, Writing, and Managing Streams
- Python File I/O: Mastering Input and Output Operations