Turning Learners Into Developers
Codekilla
CODEKILLA
back to course
Lesson 04 / 3910%· free preview
Getting Started4/5

C Input & Output

Master input and output in C using printf(), scanf(), and escape sequences for effective interaction.

Introduction

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>
1. Output in C using printf()

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:

c
printf("Hello");

Incorrect:

c
printf(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.

2. New Line in C (\n)

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 SequenceDescriptionExample CodeOutput
\nNew lineprintf("Hello\nWorld");Hello <br> World
\tHorizontal tabprintf("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;
}
Output
text
Hello
World
Hello	World
This is a backslash: \
She said "Hello"
3. Input in C using scanf()

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

SpecifierData TypeExample CodeOutput
%dIntegerprintf("Value: %d", 10);Value: 10
%fFloatprintf("Value: %f", 3.14);Value: 3.140000
%cCharacterprintf("Value: %c", 'A');Value: A
%sStringprintf("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;
}
Output
text
Integer: 10
Float: 3.140000
Character: A
String: Hello
Common Mistakes
  • Forgetting & (address operator) in scanf() calls
  • Using the wrong format specifier for the data type
  • Omitting quotes in printf()
  • Typing /n instead of \n for new lines
Quick Revision
  • printf() is used for output.
  • scanf() is used for input.
  • \n creates a new line.
  • Escape sequences help format the output.
  • Always include #include <stdio.h> at the start of your C programs.
Key Takeaways
  1. printf() displays output, and scanf() accepts user input in C.
  2. Always use the correct format specifiers for the variable types.
  3. Escape sequences like \n and \t format the output for clarity.
  4. Use & before variable names in scanf() (except for strings).
  5. Missing quotes or incorrect format specifiers will result in errors.
Interview Questions

Practice Questions
  1. Write a C program to input two numbers and print their sum.
  2. Demonstrate the use of three different escape sequences in printf().
  3. Create a program that reads a character and displays it back.
  4. Modify a program to print output on three different lines using both single and multiple printf() statements.
  5. Write a program that accepts a user's name as input and displays a welcome message.
Pro Tips
  1. Always match format specifiers with variable types in both printf() and scanf().
  2. Use separate printf() statements for readability when printing multiple lines.
  3. Remember to use \n for neat, readable output in the console.
  4. Never forget the & operator for input variables in scanf(), except with strings.
  5. Comment your code for clarity, especially when demonstrating input and output operations.
AI-powered recap

Quick recap quiz?

We'll generate 5 MCQs from this lesson and check your understanding instantly. Takes ~30 seconds.

# program

Program

C
#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;
}
Ready to move on?
// example library
Want more hands-on snippets in C?
Browse 360 runnable examples · across 36 chapters · short, copy-paste-friendly · grouped by topic
Explore examples
// suggested companion course
Data Structures with C
Comfortable with C? Level up — arrays · linked lists · stacks · queues · trees · graphs · sorting algorithms.
Open course
// side-by-side reference
See this in other languages
Compare the same concept across C, C++, Java, and Python — one table, zero tab-switching.
Compare Languages
// feedback.matters()
Did this lesson help you?