3 Input And Output

C Programming · Unit 3 · 2 hrs

Input and Output

Exam-focused notes for Input and Output (C Programming, CSC115): what the TU syllabus asks and how it has actually been tested, with 3 solved past questions from this unit.

Input and Output in short

This chapter covers how a C program reads data from the keyboard and writes it back to the screen, and the format specifiers that control exactly how that data is presented.

Input and Output: notes

Conversion Specification

Whenever data is read in or written out, C must be told what type that data is. A conversion specification, or format specifier, is a character sequence beginning with % that names the type of value being processed, for example %d for an integer, %f for a floating point number, %c for a single character and %s for a string.

SpecifierReads / writes
%dsigned decimal integer
%uunsigned decimal integer
%ffloat
%lfdouble (input only; printf uses %f for both)
%csingle character
%sstring
%xhexadecimal integer
%ooctal integer

Reading a Character

A single character can be read from the keyboard either with scanf("%c", &ch) or with the library function getchar(), which takes no arguments and returns the next character typed:

#include <stdio.h>

int main() {
    char ch;
    printf("Enter a character: ");
    ch = getchar();
    printf("You entered: %c\n", ch);
    return 0;
}

Because getchar() reads one character at a time, the newline produced when the user presses Enter is itself read as a character on the next call, which is a common source of unexpected blank reads if it is not accounted for.

What this unit covers

  • Conversion specification
  • Reading a character
  • Writing a character
  • I/O operations
  • Formatted I/O

Formatted I/O

2081 · 5 marks

Describe different formatted input and output functions. Why do we use them? [4+1]

Formatted input and output means that data is entered and displayed in a particular format. Through format specifications, better presentation of results can be obtained. Formatted I/O functions allow reading and writing data in a structured, human-readable form using format specifiers (such as %...

Open this answer on its own page →

2078 · 5 marks

Explain formatted I/O functions in detail. [5]

Formatted I/O functions are file input/output functions that allow reading and writing of formatted data (data with specific format specifiers like %d, %f, %s, etc.) to and from files. They work in text mode, meaning data is stored as characters and conversion is required when reading back. There...

Open this answer on its own page →

I/O operations

2077 · 5 marks

What do you mean by unformatted I/O? Explain. [5]

Show solved answer

Unformatted I/O in C

Definition

Unformatted I/O refers to input/output functions in C that read or write data without any format specification. These functions do not require a format string (like %d, %f, etc.) and deal with raw characters or strings directly. They are simpler and faster than formatted I/O functions like scanf() and printf().


Key Characteristics

  • No format specifiers are used.
  • They work directly with characters and strings.
  • They are declared in the header file <stdio.h>.
  • They are mainly used for simple character-level or string-level input and output.

Unformatted I/O Functions in C

1. Character Input/Output Functions

FunctionPurpose
getchar()Reads a single character from standard input (keyboard)
putchar()Writes a single character to standard output (screen)

Syntax:

char ch;
ch = getchar();   // reads one character
putchar(ch);      // prints one character

Example:

#include <stdio.h>
int main() {
    char ch;
    ch = getchar();    // user types a character
    putchar(ch);       // prints that character
    return 0;
}

2. String Input/Output Functions

FunctionPurpose
gets()Reads a string (including spaces) from standard input
puts()Writes a string to standard output followed by a newline

Syntax:

char name[50];
gets(name);     // reads a full line as string
puts(name);     // prints the string

Example:

#include <stdio.h>
int main() {
    char name[50];
    gets(name);     // reads string with spaces
    puts(name);     // prints string and moves to new line
    return 0;
}

Difference: Unformatted vs Formatted I/O

FeatureUnformatted I/OFormatted I/O
Format specifierNot requiredRequired (%d, %f, etc.)
Functionsgetchar(), putchar(), gets(), puts()scanf(), printf()
Data type handledCharacters and strings onlyAny data type
ComplexitySimpleMore flexible

Summary

Unformatted I/O functions are used when the program needs to handle raw characters or strings without worrying about data types or format specifiers. They are straightforward, easy to use, and suitable for simple input/output operations in C programs.

Open this answer on its own page →