C Input & Output
Master input and output in C using printf(), scanf(), and escape sequences for effective interaction.
In C programming, input and output are core functionalities that allow your program to interact with users.
- Output: Display data using the
printf()function. - Input: Receive data using the
scanf()function.
Both functions are defined in the standard input-output header:
c#include <stdio.h>
The printf() function is used to print text or values on the screen.
c#include <stdio.h> int main() { printf("Hello World!"); return 0; }
Rules for printf()
- Text must be surrounded by double quotes
" ". - Omitting the quotes leads to a compilation error.
Correct:
cprintf("Hello");
Incorrect:
cprintf(Hello);
Multiple printf() Statements
You can use multiple printf() statements to print several lines.
c#include <stdio.h> int main() { printf("Hello World!"); printf("I am learning C."); return 0; }
By default, printf() does not add a new line between outputs.
To print output on a new line, use the newline escape sequence \n. This moves the cursor to the beginning of the next line.
c#include <stdio.h> int main() { printf("Hello World!\n"); printf("I am learning C."); return 0; }
Multiple Lines in Single printf()
You can print multiple lines using a single printf() statement by inserting \n where you want line breaks.
c#include <stdio.h> int main() { printf("Hello World!\nI am learning C.\nAnd it is awesome!"); return 0; }
Note: This method works, but using multiple
printf()statements often makes code more readable.
Creating a Blank Line
To create a blank line between outputs, use two \n escape sequences.
c#include <stdio.h> int main() { printf("Hello World!\n\n"); printf("I am learning C."); return 0; }
What is \n Exactly?
\n is called an escape sequence in C.
Escape sequences:
- Start with a backslash (
\). - Represent special characters.
- Help format output.
Specifically, \n moves the cursor to the next line.
Common Escape Sequences in C
| Escape Sequence | Description | Example Code | Output |
|---|---|---|---|
| \n | New line | printf("Hello\nWorld"); | Hello <br> World |
| \t | Horizontal tab | printf("Hello\tWorld"); | Hello World |
| \ | Prints backslash () | printf("This is a backslash: \"); | This is a backslash: \ |
| " | Prints double quote (") | printf("She said "Hello""); | She said "Hello" |
Example Using Escape Sequences
c#include <stdio.h> int main() { printf("Hello\nWorld\n"); printf("Hello\tWorld\n"); printf("This is a backslash: \\\n"); printf("She said \"Hello\"\n"); return 0; }
textHello World Hello World This is a backslash: \ She said "Hello"
The scanf() function is used to receive input from the user.
c#include <stdio.h> int main() { int num; printf("Enter a number: "); scanf("%d", &num); printf("You entered: %d", num); return 0; }
Rules for Using scanf()
- Use the correct format specifiers (
%d,%f,%c, etc.). - Always use
&(address operator) before the variable name (except for strings). - Input must match the variable's data type.
Common Format Specifiers in C
| Specifier | Data Type | Example Code | Output |
|---|---|---|---|
| %d | Integer | printf("Value: %d", 10); | Value: 10 |
| %f | Float | printf("Value: %f", 3.14); | Value: 3.140000 |
| %c | Character | printf("Value: %c", 'A'); | Value: A |
| %s | String | printf("Value: %s", "Hello"); | Value: Hello |
c#include <stdio.h> int main() { int num = 10; float pi = 3.14; char ch = 'A'; char str[] = "Hello"; printf("Integer: %d\n", num); printf("Float: %f\n", pi); printf("Character: %c\n", ch); printf("String: %s\n", str); return 0; }
textInteger: 10 Float: 3.140000 Character: A String: Hello
- Forgetting
&(address operator) inscanf()calls - Using the wrong format specifier for the data type
- Omitting quotes in
printf() - Typing
/ninstead of\nfor new lines
printf()is used for output.scanf()is used for input.\ncreates a new line.- Escape sequences help format the output.
- Always include
#include <stdio.h>at the start of your C programs.
printf()displays output, andscanf()accepts user input in C.- Always use the correct format specifiers for the variable types.
- Escape sequences like
\nand\tformat the output for clarity. - Use
&before variable names inscanf()(except for strings). - Missing quotes or incorrect format specifiers will result in errors.
- Write a C program to input two numbers and print their sum.
- Demonstrate the use of three different escape sequences in printf().
- Create a program that reads a character and displays it back.
- Modify a program to print output on three different lines using both single and multiple printf() statements.
- Write a program that accepts a user's name as input and displays a welcome message.
- Always match format specifiers with variable types in both printf() and scanf().
- Use separate printf() statements for readability when printing multiple lines.
- Remember to use
\nfor neat, readable output in the console. - Never forget the
&operator for input variables in scanf(), except with strings. - Comment your code for clarity, especially when demonstrating input and output operations.
Quick recap quiz?
We'll generate 5 MCQs from this lesson and check your understanding instantly. Takes ~30 seconds.
Program
#include <stdio.h>
int main(void) {
int age;
float height;
char initial;
char name[50];
printf("Enter your age: ");
scanf("%d", &age);
printf("Enter your height in meters: ");
scanf("%f", &height);
printf("Enter the first letter of your name: ");
scanf(" %c", &initial); // Space before %c to consume leftover newline
printf("Enter your name: ");
scanf("%s", name);
printf("\nSummary:\n");
printf("Name: %s\n", name);
printf("Initial: %c\n", initial);
printf("Age: %d\n", age);
printf("Height: %.2f meters\n", height);
printf("\nWelcome, %s!\n", name);
return 0;
}