Input and Output in C Practice Questions with Solutions

Introduction

Input and output are essential parts of C programming. Input allows a program to receive data from the user, while output displays information on the screen. In this chapter, you will practice printf(), scanf(), getchar(), and putchar() through simple programs. The examples begin with basic output and gradually move to user input, calculations, characters, and practical programs so beginners can understand how data moves into and out of a C program. Input and Output in C Practice questions with solutions to help you understand the concepts.

Q1. Display a Message Using printf()

Problem Statement

Write a C program to display a welcome message on the screen using printf().

C Program

#include <stdio.h>

int main()
{
    printf("Welcome to C Programming!");

    return 0;
}

Sample Output

Welcome to C Programming!

Concepts Covered

  • printf()
  • Standard output
  • stdio.h
  • main()

Q2. Display Multiple Lines

Problem Statement

Write a C program to display your name, age, and city on separate lines.

C Program

#include <stdio.h>

int main()
{
    printf("Name: Rahul\n");
    printf("Age: 15\n");
    printf("City: Delhi");

    return 0;
}

Sample Output

Name: Rahul
Age: 15
City: Delhi

Concepts Covered

  • printf()
  • \n
  • Multiple output statements
  • Formatted output

Q3. Take an Integer as Input

Problem Statement

Write a C program that asks the user to enter their age and then displays it.

C Program

#include <stdio.h>

int main()
{
    int age;

    printf("Enter your age: ");
    scanf("%d", &age);

    printf("Your age is %d", age);

    return 0;
}

Sample Output

Enter your age: 18
Your age is 18

Concepts Covered

  • scanf()
  • %d
  • Integer input
  • Address-of operator &
  • printf()

Q4. Take Two Numbers and Display Their Sum

Problem Statement

Write a C program that accepts two integers from the user and displays their sum.

C Program

#include <stdio.h>

int main()
{
    int a, b, sum;

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

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

    sum = a + b;

    printf("Sum = %d", sum);

    return 0;
}

Sample Output

Enter first number: 25
Enter second number: 15
Sum = 40

Concepts Covered

  • Integer input
  • Multiple variables
  • scanf()
  • Addition
  • Output using printf()

Q5. Take a Decimal Number as Input

Problem Statement

Write a C program that accepts the price of a product and displays it with two decimal places.

C Program

#include <stdio.h>

int main()
{
    float price;

    printf("Enter product price: ");
    scanf("%f", &price);

    printf("Product price = %.2f", price);

    return 0;
}

Sample Output

Enter product price: 499.5
Product price = 499.50

Concepts Covered

  • float
  • %f
  • Decimal input
  • Decimal formatting
  • scanf()

Q6. Take a Character as Input

Problem Statement

Write a C program that accepts a character from the user and displays it.

C Program

#include <stdio.h>

int main()
{
    char grade;

    printf("Enter your grade: ");
    scanf(" %c", &grade);

    printf("Your grade is %c", grade);

    return 0;
}

Sample Output

Enter your grade: A
Your grade is A

Concepts Covered

  • char
  • %c
  • Character input
  • scanf()
  • printf()

Q7. Read a Name Using scanf()

Problem Statement

Write a C program that accepts a person’s first name and displays it.

C Program

#include <stdio.h>

int main()
{
    char name[50];

    printf("Enter your name: ");
    scanf("%49s", name);

    printf("Hello, %s!", name);

    return 0;
}

Sample Output

Enter your name: Rahul
Hello, Rahul!

Concepts Covered

  • Character array
  • String input
  • %s
  • scanf()
  • printf()

%s with scanf() reads a word up to whitespace. The 49 in %49s helps prevent writing beyond the 50-character array.


Q8. Read a Character Using getchar()

Problem Statement

Write a C program that accepts one character using getchar() and displays it using putchar().

C Program

#include <stdio.h>

int main()
{
    char ch;

    printf("Enter a character: ");
    ch = getchar();

    printf("You entered: ");
    putchar(ch);

    return 0;
}

Sample Output

Enter a character: A
You entered: A

Concepts Covered

  • getchar()
  • putchar()
  • Character input
  • Character output

Q9. Calculate the Area Using User Input

Problem Statement

Write a C program that asks the user for the length and width of a rectangle and displays its area.

C Program

#include <stdio.h>

int main()
{
    float length, width, area;

    printf("Enter length: ");
    scanf("%f", &length);

    printf("Enter width: ");
    scanf("%f", &width);

    area = length * width;

    printf("Area of rectangle = %.2f", area);

    return 0;
}

Sample Output

Enter length: 12.5
Enter width: 5
Area of rectangle = 62.50

Concepts Covered

  • User input
  • float
  • scanf()
  • Arithmetic calculation
  • printf()

Q10. Create a Student Information Input/Output Program

Problem Statement

Create a C program that accepts a student’s name, age, marks, and grade, then displays all the information.

C Program

#include <stdio.h>

int main()
{
    char name[50];
    int age;
    float marks;
    char grade;

    printf("Enter student name: ");
    scanf("%49s", name);

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

    printf("Enter marks: ");
    scanf("%f", &marks);

    printf("Enter grade: ");
    scanf(" %c", &grade);

    printf("\nStudent Information\n");
    printf("-------------------\n");
    printf("Name  : %s\n", name);
    printf("Age   : %d\n", age);
    printf("Marks : %.2f\n", marks);
    printf("Grade : %c\n", grade);

    return 0;
}

Sample Output

Enter student name: Rahul
Enter age: 15
Enter marks: 87.5
Enter grade: A

Student Information
-------------------
Name  : Rahul
Age   : 15
Marks : 87.50
Grade : A

Concepts Covered

  • Multiple inputs
  • int
  • float
  • char
  • Character array
  • scanf()
  • printf()
  • Formatted output

Common Input and Output Functions

FunctionPurposeExample
printf()Displays formatted outputprintf("%d", age);
scanf()Reads formatted inputscanf("%d", &age);
getchar()Reads one characterch = getchar();
putchar()Displays one characterputchar(ch);

Important Format Specifiers

SpecifierCommon UseExample
%dint25
%ffloat input / floating output25.50
%cCharacterA
%sStringRahul
%lfdouble input with scanf()25.50

Key Takeaways

  • printf() in C is commonly used to display formatted output.
  • scanf() in C is commonly used to take formatted input.
  • getchar() in C reads one character from standard input.
  • putchar() writes one character to standard output.
  • %d is commonly used with int.
  • %f is used for float input with scanf() in C.
  • %c is used for character input and output.
  • %s is used for strings.
  • The & operator is generally required with scanf() in C when reading into ordinary scalar variables such as int, float, and char.
  • A character array used with %s in scanf() in C is passed without &.
  • Input and output functions are declared in <stdio.h>.

FAQs

1. What is input in C?

Input is data supplied to a program, usually through devices such as the keyboard. Functions such as scanf() and getchar() can read input from standard input.

2. What is output in C?

Output is information produced by a program and displayed through standard output. printf() and putchar() are commonly used for this purpose.

3. What is printf() used for in C?

printf() displays formatted text and values on the screen.

Example:

int age = 18;
printf("Age = %d", age);

4. What is scanf() used for in C?

scanf() reads formatted input from standard input.

Example:

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

5. Why is & used with scanf() in C?

For most scalar variables, scanf() needs the variable’s address so it knows where to store the input value.

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

Here, &age provides the address of age.

6. What is the difference between getchar() and scanf("%c", &ch)?

Both can read a character. getchar() directly reads one character from standard input, while scanf("%c", &ch) uses the formatted-input mechanism.

A leading space in " %c" is often used with scanf() when you want it to skip whitespace characters left in the input stream.

7. Can scanf("%s") read a full name with spaces?

No. %s stops reading when it encounters whitespace.

For example, entering:

Rahul Kumar

with %s reads only:

Rahul

For complete lines containing spaces, functions such as fgets() are more appropriate.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top