In C programming, input and output are basic operations.

These functions are defined in:
#include <stdio.h>

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()

✅ 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

#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:

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
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

#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;
}
#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()

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

Quick Revision