Module 2: Chapter 8 — Console I/O

Console I/O in C

C does not have keywords for input/output. Instead, all I/O operations are performed using functions defined in the <stdio.h> header. Console I/O refers to reading input from the keyboard and displaying output on the screen.

Key Points:

Reading and Writing Characters

The simplest form of input/output in C deals with single characters. The functions getchar() and putchar() are used for this purpose.

Example:

#include <stdio.h>
int main() {
  char ch;
  printf("Enter a character: ");
  ch = getchar();
  printf("You entered: ");
  putchar(ch);
  return 0;
}

Explanation:

Reading and Writing Strings

To handle multiple characters (strings), C provides gets() and puts(). These are used to read and display entire lines of text.

Example:

#include <stdio.h>
int main() {
  char name[50];
  printf("Enter your name: ");
  gets(name); // use fgets(name, 50, stdin) instead
  puts("Hello, ");
  puts(name);
  return 0;
}

Formatted Console I/O

The most commonly used I/O functions in C are printf() and scanf(), which allow formatted input and output.

Example:

#include <stdio.h>
int main() {
  int age;
  float salary;
  printf("Enter your age and salary: ");
  scanf("%d %f", &age, &salary);
  printf("Age: %d, Salary: %.2f\\n", age, salary);
  return 0;
}

Common Format Specifiers:

Key Notes:

Summary of Console I/O Functions

Function Purpose Defined In
getchar() Reads a single character from the keyboard <stdio.h>
putchar() Displays a single character on screen <stdio.h>
gets() Reads a string (unsafe, use fgets()) <stdio.h>
puts() Displays a string followed by newline <stdio.h>
printf() Formatted output <stdio.h>
scanf() Formatted input <stdio.h>

Formatted Console I/O

C provides two powerful standard functions for formatted input and output: printf() and scanf(). These allow the programmer to control the layout, format, and conversion of input and output data.

printf( ) — Formatted Output Function

What is it?

The printf() function is used to print data to the console (standard output). It allows text and variable values to be displayed in a formatted manner according to specified conversion codes.

Common Format Specifiers:

Example:

#include <stdio.h>
int main() {
  char ch = 'A';
  int num = 100;
  float pi = 3.1416;

  printf("Character: %c\\n", ch);
  printf("Integer: %d\\n", num);
  printf("Floating-point: %.2f\\n", pi);
  printf("Hexadecimal: %x\\n", num);
  return 0;
}

Character: A
Integer: 100
Floating-point: 3.14
Hexadecimal: 64

Format Modifiers:

Example of Field Width & Precision:

#include <stdio.h>
int main() {
  double num = 10.12345;

  printf("Default: %f\\n", num);
  printf("Width 10: %10f\\n", num);
  printf("Precision 2: %.2f\\n", num);
  printf("Zero padded: %010.2f\\n", num);
  return 0;
}

Default: 10.123450
Width 10:     10.123450
Precision 2: 10.12
Zero padded: 000010.12

Key Notes:

scanf( ) — Formatted Input Function

What is it?

The scanf() function is used to read formatted input from the standard input device (keyboard). It reads different data types based on the format specifiers and stores them in corresponding variables.

Common Format Specifiers:

Example:

#include <stdio.h>
int main() {
  int age;
  float marks;
  char grade;

  printf("Enter age, marks, and grade: ");
  scanf("%d %f %c", &age, &marks, &grade);

  printf("You entered: %d, %.2f, %c\\n", age, marks, grade);
  return 0;
}

Enter age, marks, and grade: 20 85.5 A
You entered: 20, 85.50, A

Important Points:

Example: Reading Strings and Numbers

#include <stdio.h>
int main() {
  char name[20];
  int age;

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

  printf("Hello %s, you are %d years old.\\n", name, age);
  return 0;
}

Enter your name and age: Alice 22
Hello Alice, you are 22 years old.

Key Notes: