Turning Learners into Developers
- Introduction
In C programming, input and output are basic operations.
- Output → Display data using printf()
- Input → Take user data using scanf()
These functions are defined in:
#include <stdio.h>- 1. Output in C using printf()
The printf() function in C is used to print text or values on the screen.
#include <stdio.h>
int main() {
printf("Hello World!");
return 0;
}Rules for printf()
- Text must be inside double quotes " "
- Missing quotes → ❌ error
✅ Correct:
printf("Hello");❌ Wrong:
printf(Hello);Multiple printf() Statements
#include <stdio.h>
int main()
{
printf("Hello World!");
printf("I am learning C.");
return 0;
}By default, no new line is added
- 2. New Line in C (\n)
- To print output on a new line, we use the newline escape sequence \n.
- It moves the cursor to the beginning of the next line.
#include <stdio.h>
int main()
{
printf("Hello World!\n");
printf("I am learning C.");
return 0;
}Multiple Lines in Single printf()
You can also print multiple lines using a single printf() statement.
#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 makes code more readable. Creating a Blank Line
Note: This method works, but using multiple printf() statements makes code more readable.
#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 in formatting 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 |
\t |
| 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
#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;
}- 3. Input in C using scanf()
- The scanf() function is used to take input from the user.
#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 correct format specifiers (%d, %f, %c, etc.)
- Always use & (address operator) before variable name (except strings)
- Input must match the 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 |
#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;
}Common Mistakes
- Missing &(Address Operator) in scanf()
- Wrong format specifier
- Missing quotes in printf()
- Writing /n instead of \n
Quick Revision
- printf() → Used for output
- scanf() → Used for input
- \n → New line
- Escape sequences → Format output
- Always include
01
C Introduction
02
C Get Started
03
C Syntax
04
C Input & Output
05
C Comments
06
C Variables
07
C Data Types
08
C Constants
09
C Operators
10
C Booleans
11
C Type Conversion
12
C If...Else
13
C Switch
14
C For Loop
15
C While Loop
16
C Do/While Loop
17
C Nested For Loop
18
C Break/Continue
19
C Arrays
20
C Strings
21
C User Input
22
C Memory Address
23
C Pointers
24
C Functions
25
C Files
26
C Structures
27
C Enums
28
C Memory
29
C Errors
30
C More
31
C Projects
32
C Reference
33
C Examples
34
DS With C
35
C Exam Questions
36