BCA TU, C Programming – Unit 5: Input and Output

In C programming, Input/Output (I/O) operations allow us to communicate with the user. Input refers to getting data from the user, while output refers to displaying data to the user. This unit focuses on both formatted and unformatted I/O functions.


1. Input/Output Operations in C

  • Input: Data entered by the user (keyboard, file, etc.).
  • Output: Data displayed to the user (screen, printer, file).
  • Handled through standard library functions defined in <stdio.h>.

2. Formatted I/O Functions

Formatted I/O functions allow input/output in a specific format.

(a) printf() – Formatted Output

  • Used to display output on the screen.

Syntax:

printf(“format string”, variable_list);

Example:

int age = 20;

float pi = 3.1416;

printf(“Age = %d, PI = %.2f”, age, pi);

 Output:

Age = 20, PI = 3.14

Common Format Specifiers:

  • %d → integer
  • %f → float
  • %c → character
  • %s → string
  • %.2f → float with 2 decimal places

(b) scanf() – Formatted Input

  • Used to take input from the user.

Syntax:

scanf(“format string”, &variable_list);

Example:

int roll;

scanf(“%d”, &roll);

printf(“Roll Number = %d”, roll);

⚠️ Important: Use & (address-of operator) for variables.


3. Unformatted I/O Functions

These functions are used for single character or string input/output, without format control.

(a) getchar() – Input Single Character

  • Reads one character from the keyboard.

char c;

c = getchar();

printf(“Character entered: %c”, c);

(b) putchar() – Output Single Character

  • Displays one character on screen.

char c = ‘A’;

putchar(c);   // Output: A


(c) getch() – Input Character (No Echo)

  • Reads a single character without displaying it on screen (commonly used for passwords).

char ch;

ch = getch();   // User presses ‘X’

printf(“%c”, ch);  // Output: X

(d) putch() – Output Character

  • Prints a single character (similar to putchar()).

putch(‘Z’);  // Output: Z


(e) gets() – Input String (deprecated, but still asked in exams)

  • Reads a line of text (string) from the user.

char name[30];

gets(name);

printf(“Hello %s”, name);

⚠️ Problem: gets() is unsafe because it allows buffer overflow. Use fgets() in modern C.

(f) puts() – Output String

  • Displays a string followed by a newline.

char city[] = “Kathmandu”;

puts(city);   // Output: Kathmandu


4. Difference Between Formatted & Unformatted I/O

AspectFormatted I/O (printf, scanf)Unformatted I/O (getchar, putchar, gets, puts)
ControlProvides formatting (e.g., decimals, alignment).No control, direct input/output.
Data TypesSupports multiple types (int, float, char, string).Usually deals with characters and strings.
SafetySafer (with format specifiers).Risk of overflow (gets).
Examplescanf(“%d”, &a);a = getchar();

5. Example Program – Input & Output

#include <stdio.h>

int main() {

    int age;

    char name[20];

    printf(“Enter your name: “);

    gets(name);  // Taking string input

    printf(“Enter your age: “);

    scanf(“%d”, &age);

    printf(“\nHello %s, you are %d years old.”, name, age);

    return 0;

}

Output:

Enter your name: Sita

Enter your age: 21

Hello Sita, you are 21 years old.


Key Takeaways

  1. Formatted I/O → printf(), scanf() for structured input/output.
  2. Unformatted I/O → getchar(), putchar(), gets(), puts(), getch(), putch().
  3. Always use & with variables in scanf().
  4. gets() is unsafe → use fgets() in modern C.
  5. I/O functions are essential for interactive C programs.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.