Lesson 13 of 60 – Input and Output in C
22%

Input and Output in C

Input and output are important parts of programming. Input allows a program to receive data from the user, while output allows the program to display information. In C, functions such as printf() and scanf() are commonly used for formatted output and input.

Note: The standard input/output functions used in this lesson are declared in <stdio.h>.

1. What is Input?

Input is data provided to a program while it is running. For example, a user can enter their name, age, marks, or other information.

Enter your age: 20

Here, 20 is input provided by the user.

2. What is Output?

Output is information produced by a program and displayed on a screen or sent to another output device.

Hello World

A C program commonly displays output using printf().

3. Standard Input and Output

C provides standard input/output facilities through the <stdio.h> header.

#include <stdio.h>

This header provides declarations for functions such as printf(), scanf(), getchar(), and putchar().

4. The printf() Function

The printf() function is used to display formatted output.

#include <stdio.h>

int main()
{
    printf("Hello World");

    return 0;
}

Output:

Hello World

5. Printing Text

Text can be printed by placing it inside double quotation marks.

printf("Welcome to C Programming");

Output:

Welcome to C Programming

6. Printing an Integer

The %d conversion specifier can be used to display an int.

int age = 20;

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

Output:

Age = 20

7. Printing a Float

Floating-point values can be displayed using %f.

float price = 99.50f;

printf("Price = %f", price);

8. Printing a Character

The %c conversion specifier can be used to display a character.

char grade = 'A';

printf("Grade = %c", grade);

Output:

Grade = A

9. Printing a String

The %s conversion specifier can be used to display a null-terminated character string.

char name[] = "Rahul";

printf("Name = %s", name);

10. New Line with \n

The escape sequence \n moves the cursor to the next line.

printf("Hello\nWorld");

Output:

Hello
World

11. Printing Multiple Values

A single printf() call can display multiple values.

int age = 20;
int marks = 85;

printf("Age = %d, Marks = %d", age, marks);

12. What is scanf()?

The scanf() function is used to read formatted input from the standard input stream, normally the keyboard.

int age;

scanf("%d", &age);

13. Reading an Integer

Use %d with scanf() to read an int.

int age;

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

The value entered by the user is stored in age.

14. Why Use & with scanf()?

For a typical scalar variable, scanf() needs the address where the input value should be stored. The address-of operator & provides that address.

int age;

scanf("%d", &age);

15. Reading a Float

Use %f in scanf() when reading a float.

float price;

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

16. Reading a Double

Use %lf in scanf() to read a double.

double salary;

printf("Enter salary: ");
scanf("%lf", &salary);

17. Reading a Character

The %c conversion specification can be used to read a character.

char grade;

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

The leading space before %c tells scanf() to skip leading whitespace.

18. Reading a String

A character array can be used to store a string.

char name[20];

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

The array name already represents the address of its first element, so & is not used here.

19. scanf() Reads a Word with %s

With %s, scanf() normally reads characters until whitespace is encountered.

char name[30];

scanf("%29s", name);

For example, entering Rahul works, but a multi-word name would be split at the whitespace.

20. Complete Input Example

#include <stdio.h>

int main()
{
    int age;

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

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

    return 0;
}

21. Input and Output Together

A program can receive input and then display the result.

#include <stdio.h>

int main()
{
    int number;

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

    printf("You entered: %d", number);

    return 0;
}

22. Reading Two Integers

Multiple values can be read using multiple conversion specifications.

int a, b;

printf("Enter two numbers: ");
scanf("%d %d", &a, &b);

The user can enter the values separated by whitespace.

23. Addition Using Input

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

24. Input for Floating-Point Values

#include <stdio.h>

int main()
{
    float price, quantity;

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

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

    printf("Total = %.2f", price * quantity);

    return 0;
}

25. putchar() Function

The putchar() function writes a single character to standard output.

#include <stdio.h>

int main()
{
    putchar('A');

    return 0;
}

Output:

A

26. getchar() Function

The getchar() function reads one character from standard input.

#include <stdio.h>

int main()
{
    char ch;

    ch = getchar();

    printf("You entered: %c", ch);

    return 0;
}

27. Output Formatting

The printf() function supports width and precision to control formatted output.

float price = 99.5678;

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

Output:

99.57

28. Checking scanf() Result

scanf() returns the number of input items successfully assigned. This can be used to check whether the expected input was received.

int age;

if (scanf("%d", &age) == 1)
{
    printf("Valid input");
}

29. Common Input and Output Mistakes

  • Forgetting #include <stdio.h>.
  • Using the wrong format specifier.
  • Forgetting & when it is required by scanf().
  • Using & incorrectly with a character array used as a string.
  • Forgetting quotation marks in printf().
  • Using an input width that does not safely fit the destination array.

30. Complete Input and Output Program

#include <stdio.h>

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

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

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

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

    printf("\n--- Student Details ---\n");
    printf("Name = %s\n", name);
    printf("Age = %d\n", age);
    printf("Marks = %.2f\n", marks);

    return 0;
}

This program demonstrates basic keyboard input and formatted output using different data types.

📌 Key Points

  • Input allows a program to receive data.
  • Output allows a program to display information.
  • <stdio.h> provides standard input/output functions.
  • printf() is commonly used for formatted output.
  • scanf() is commonly used for formatted input.
  • %d is commonly used for int.
  • %f is used to read a float with scanf().
  • %lf is used to read a double with scanf().
  • %c is used for characters and %s for strings.
  • getchar() reads one character and putchar() writes one character.
  • Always use a format specification that is compatible with the data being read or displayed.

🧠 Quick Quiz

Question: Which function is commonly used to take formatted input from the user in C?