2.7 INPUT AND OUTPUT STATEMENTS

 

     Programming in C    


INPUT / OUTPUT IN C

BASIC SCREEN AND KEYBOARD

C provides several input and output functions that give different levels of I/O capability. These functions are, in most cases, implemented as library routines that internally call lower-level input/output functions.

The input and output functions in C are built around the concept of a set of standard data streams that connect each executing program to the basic input/output devices. These standard data streams or files are opened automatically by the operating system and are available to every C program without explicitly opening or closing them.


Standard Data Streams in C

The standard data streams available in C are:

  • stdin → connected to the keyboard

  • stdout → connected to the screen

  • stderr → connected to the screen (used for error messages)

Additional Data Streams (MS-DOS Based Systems Only)

The following streams are available only on MS-DOS based computers and not on UNIX or other multi-user operating systems:

  • stdaux → connected to the first serial communication port

  • stdprn → connected to the first parallel printer port

A number of functions and macros exist to provide support for streams of various kinds. The <stdio.h> header file contains the necessary function declarations, macros, and type definitions for input and output operations.


Classification of Input and Output Functions

The input/output functions in C fall into two categories:

  1. Non-formatted input and output functions

  2. Formatted input and output functions


Note

  • The input and output functions in C are implemented through standard data streams.

  • Input/output functions are classified into non-formatted and formatted functions.




1. NON-FORMATTED INPUT AND OUTPUT

Non-formatted input and output operations are carried out using standard input-output library functions. These functions handle one character at a time.

  • Input functions do not require pressing <Enter> immediately after typing a character.

  • Output functions display a single character on the console.



1.1 SINGLE CHARACTER INPUT AND OUTPUT

Character-oriented input and output are handled using the following functions:

int getchar(void); // character input int putchar(int c); // character output
  • getchar() reads a single character from the keyboard.

  • putchar() writes a single character to the display screen.

Two additional functions, gets() and puts(), are used for string input and output, which are discussed in later chapters.


1.2 SINGLE CHARACTER INPUT – getchar()

The getchar() function reads an unsigned character from the input stream stdin and converts it into an int.

  • On successful input, the character is returned.

  • On end-of-file, the function returns EOF.

  • On error, the error indicator is set.

  • Characters are read sequentially on successive calls.

General Form

char_variable = getchar();

Although input appears to be of character type, it is actually stored as an integer. Hence, the variable receiving the value must be of type int.

Important Observation

  • getchar() must distinguish between a valid character and EOF.

  • Since char cannot store EOF, the variable must be declared as int.

  • The character is stored in the lower-order byte of the integer variable.

Also, getchar() does not return the character to the program until the Enter key (\n) is pressed.


1.3 SINGLE CHARACTER OUTPUT – putchar()

The putchar() function writes a single character to the stdout stream.

  • On success, it returns the character printed.

  • On error, it returns EOF.

  • There is no end-of-file condition for output streams.

General Form

putchar(char_variable);

The character stored in the lower-order byte of the variable is displayed on the screen.


1.4 ADDITIONAL SINGLE CHARACTER INPUT / OUTPUT FUNCTIONS

The following functions are available only in Turbo C and require the header file <conio.h>:

  • getch()
    Reads a character without echoing it on the screen.

  • getche()
    Reads a character with echo on the screen.

  • putch()
    Writes a character directly to the screen.

All these functions handle data in ASCII format.


Examples Using getchar() and putchar()

  1. Display a given character
    Assigns a character to an integer variable and displays it using putchar().

  2. Display a keyed-in character
    Reads a character using getchar() and prints it using putchar().

  3. Display the next ASCII character
    Increments the ASCII value and prints the next character.

  4. Display keyed-in character and its next ASCII value
    Demonstrates ASCII arithmetic.

  5. Demonstration of increment and decrement operators
    Shows pre-increment, post-increment, and post-decrement behavior.

  6. Nested getchar() and putchar()
    Reads and prints a character in a single statement.

  7. Convert lowercase letters to uppercase
    Uses ASCII range checking and conditional operator.

  8. Use of getch() and getche()
    Demonstrates input with and without echo.




2. FORMATTED INPUT AND OUTPUT FUNCTIONS

When input and output are required in a specific format, the standard library functions scanf() and printf() are used.

  • scanf() → formatted input

  • printf() → formatted output


2.1 OUTPUT FUNCTION – printf()





The printf() function prints data in a specified format using a control string (format string).

General Form

printf("control_string", variable1, variable2, ...);
  • The control string specifies data types and format specifiers.

  • Each % conversion code corresponds to a variable in the argument list.

  • Ordinary characters are printed as they are.

Important Points

  • Format specifiers indicate type conversion and display format.

  • Using an incorrect specifier causes incorrect output.

  • Precision, width, flags, and size modifiers provide output formatting control.


Format String Components

A printf format string consists of:

  1. Ordinary characters

  2. Conversion specifier field (%)

  3. Control codes (escape sequences)


Conclusion

The input and output statements in C provide flexible mechanisms to interact with the user through the keyboard and screen. By using standard data streams, non-formatted, and formatted functions, C allows precise control over data input, output, and presentation, making it a powerful system-level programming language.





📖 Reference

Pradip Dey & Manas Ghosh,
Computer Fundamentals and Programming in C,
Second Edition, Oxford University Press, 2018.



Aivette-coi is created with the intention of helping college students learn smartly, revise quickly, and approach exams with confidence.

With love and care,
by Aivette 💙

Comments

Popular posts from this blog

22IMC7Z2 - CONSTITUTION OF INDIA

1.1 Historical Background of the Indian Constitution: The Company Rule & The Crown Rule

1.3 Preamble and Salient Features of the Indian Constitution