Unit 4: Input / Output Statements in C | B. Sc Year: III

🧭 Introduction

In programming, a program becomes meaningful only when it can receive data from the user and display results.
This is handled using input/output (I/O) statements.

In Unit 4, you will learn:

  • How to take input from the keyboard

  • How to display output neatly on the screen

  • How to handle characters, numbers, and strings

  • How formatted input/output works in C

This unit is highly practical, and many exam questions directly ask programs from this section.


🔑 Key Concepts (Explained Simply)


1️⃣ Single Character Input and Output

C provides special functions to read and write one character at a time.

🔹 Input: getchar()

  • Reads one character from the keyboard

🔹 Output: putchar()

  • Displays one character on the screen

Example:

char ch;
ch = getchar();
putchar(ch);

✅ Useful when working with single characters instead of full strings.


2️⃣ Formatted Input Using scanf()

The scanf() function is used to take input from the user.

Syntax:

scanf("format specifier", &variable);

Common Format Specifiers:

Data TypeFormat
int%d
float%f
char%c

Example:

int age;
scanf("%d", &age);

⚠️ Remember: & (address operator) is mandatory in scanf().


3️⃣ Formatted Output Using printf()

The printf() function is used to display output.

Syntax:

printf("message", variable);

Example:

int x = 10;
printf("Value of x = %d", x);

✅ Allows formatted and aligned output.


4️⃣ String Input and Output (gets() and puts())

🔹 gets() – String Input

  • Reads a line of text

  • Stores it in a character array

char name[50];
gets(name);

⚠️ Note: gets() is unsafe, but commonly asked in exams.


🔹 puts() – String Output

  • Displays a string

  • Automatically moves to the next line

puts(name);

5️⃣ Escape Sequence Characters

Escape sequences are special characters for formatting output.

EscapeMeaning
\nNew line
\tTab
\"Double quotes
\\Backslash

Example:

printf("Name:\tRam\nRoll no:\t10");

✅ Very commonly asked in exams.


6️⃣ Difference Between scanf() and gets()

scanf()gets()
Used for numbers & wordsUsed for full strings
Stops at spaceReads entire line
SaferUnsafe
Requires format specifierNo format specifier

💡 In real programs, fgets() is preferred—but exams focus on gets().


🧩 Step‑by‑Step Exam Programs


✅ Program 1: Read and Display a Character

#include <stdio.h>

int main() {
    char ch;
    printf("Enter a character: ");
    ch = getchar();
    printf("You entered: ");
    putchar(ch);
    return 0;
}

✅ Reads a single character and displays it using putchar().


✅ Program 2: Read Two Floating Numbers and Display Sum

#include <stdio.h>

int main() {
    float a, b, sum;
    printf("Enter two numbers: ");
    scanf("%f %f", &a, &b);
    sum = a + b;
    printf("Sum = %.2f", sum);
    return 0;
}

"%.2f" prints output up to two decimal places.


✅ Program 3: Use of gets() and puts()

#include <stdio.h>

int main() {
    char name[50];
    printf("Enter your name: ");
    gets(name);
    puts("Your name is:");
    puts(name);
    return 0;
}

✅ Program 4: Read Roll Number, Name, Address (Formatted Output)

#include <stdio.h>

int main() {
    char name[30], address[50];
    int roll;

    printf("Enter name: ");
    gets(name);

    printf("Enter address: ");
    gets(address);

    printf("Enter roll number: ");
    scanf("%d", &roll);

    printf("\nName:\t%s\nAddress:\t%s\nRoll No:\t%d",
           name, address, roll);

    return 0;
}

✅ Demonstrates escape sequences and string handling.


📝 Important Exam Questions

  1. Explain scanf() and printf() with examples

  2. Write a program using getchar() and putchar()

  3. Write a program using gets() and puts()

  4. What are escape sequences? Explain any four

  5. Differentiate scanf() and gets()

  6. Write a program to read two numbers and find their sum


🎯 Exam Tips / Key Points

  • Always include:

    • #include <stdio.h>

    • main() function

    • return 0;

  • Use correct format specifiers

  • Write comments if asked

  • Print values with proper formatting

  • Programs from this unit are high scoring


✅ Short Summary

In Unit 4, you learned how C programs interact with users using input/output statements. You studied:

  • Character input/output

  • Formatted input/output

  • String input/output

  • Escape sequences

  • Exam-ready C programs

This unit is essential for writing interactive C programs and will be repeatedly used in upcoming units.

Netra Koirala

Netra Koirala

Computer Science Educator

Passionate computer science educator and author. Provides free study notes, practical guides, and tutorials for Class 9, 10, 11, 12, and B.Sc CSIT students in Nepal. Years of teaching experience in computer science fundamentals.

Computer Science notes, tutorials, MCQs, and educational resources for Nepal students. Covering Class 9, SEE preparation, Class 11, Class 12, SLC, programming, DBMS, networking, HTML, JavaScript, PHP, OOP and more.

Featured Post

Grade 10 Computer Science: Specification Grid & Model Questions

Specification Grid & Model Questions of Computer Science | Grade 10 📚 Examination Resource Specification Grid & M...

Followers