Console I/O Operations

Posted On
Socialize It →
0
Console functions perform input from the standard input device and output to the standard output device of a system.

Console word generally refers to the combination of monitor and keyboard computers context. So console I/O functions are also related to the same. Some console function that we are going to learn are getchar(), putchar(), gets(), puts(), which are console I/O  function in C++.

Single Character Function

The Simplest of the console I/O function are getchar(), which reads a character from the keyboard, a putchar() , which prints a character to the screen. Following statements reads a character using getchar() and stores it in variable Ch.

Ch=getchar() ;

The Getchar() waits for the character input until a character is typed at the keyboard. Similarly, putchar() displays the given character on the screen at the current cursor position.

Following statement displays a character (‘b’) on the screen:

Putchar (‘b’);

We can also use putchar() with variables:-

Char ch ;

Ch= getchar() ;

Putchar(ch) ;

String Functions

The functions gets() and puts () are string functions

 The gets() function accepts a string of character entered at the keyboard and places them in the string variable mentioned it.  For ex:-

Char name [21] ;

Gets (name) ;

The above code declares a string namely name which can store 20 valid characters.  The function gets() reads a string of maximum 20 characters and stores it in a memory address pointed to by name. As soon as the carriage return is pressed, a null terminator ‘/0’ is automatically placed at the end of the string.
The function puts() writes a sting on the screen and advances the cursor to the newline.  Any subsequent outputs will apper on the next line of the current output by puts().


0 comments:

Post a Comment

'