Sunday, September 8, 2019

Console I/O operations in C++ | Skillyfy


Best C++ training in Delhi


Every program takes some input and then generates the processed data as output. Every programming language provides the facilities for handling input and output operations. C++ is practically unique in its approach to input/output operations.

We are familiar with cout and cinthat use << and >> operators for output and input operations.

C++ training Programming
C++ training in Delhi

The coutand cin are defined in C++’s standard header file iostream.h.

We will talk about some console I/O functions that C++ inherits from its base language C. Console I/O functions perform input from the standard input device and output to the standard output device of a system.

Header Files and Their Purpose

We know that functions are not available as keywords in C++ rather they are available under C++ library header files.

The header files provide declaration of function prototypes.

“A function prototype is a declaration that specifies the function’s name, number and types of arguments it takes and type of return value it provides.

To make the use of predefined functions, the header file must be included in a program so that the declarations of the functions become available in that program.

#include Directive

The #include directive instructs the compiler to read another file and include that file under the current file. After including the specified file, the compiler compiles the total code . # include is called the pre-processor directive which instructs the compiler in the pre-processing stage. A pre-processing stage is the stage before the actual compilation of the program code.

A header file’s may be included in two ways as shown below:

#include<iostream.h>  //from standard include directory
#include “myheader.h” // from current directory

Benefits of Header Files

The two major safeguards for using header files are:

·        The same declarations are made available to the programs that include header files.
·        Should a declaration require updating, only one change to the header file need to be made. The changes will be automatically reflected to files that include the header file.


Unformatted Console I/O Functions.

The console I/O functions we will cover are :getchar(),putchar(),gets() and puts(). Out of these getchar() and putchar() are single character functions and gets() and puts() are string functions.

The header files used for these functions are stdio.h, therefore this file must be included in the program that uses this function.

We use getchar(), which reads a character from the keyboard, and putchar(), which prints a character to the screen . Following statement will read a character using getchar() and stores it in a 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.
Example :
Char ch;
ch = getchar();
putchar(ch);

Whatever is stored inside ch gets displayed on the screen.

gets() and puts() functions are used to read and write strings of characters at the console.
The gets function accepts a string of characters entered at the keyboard and places them in the string variable mentioned with it: For instance:

Char name[21]; // to store the string

gets(name);
the above code declares a string namely name which can store 20 valid characters.(width 21 specify extra character for ‘\0’ with which a string is always terminated).

The function puts() writes a string on the screen and advances the cursor to the newline.

puts(“check”);
We will discuss about more functions in our next blog entry.

Skillyfy provides best C++ training in Delhi. For more info visit www.skillyfy.com or call :7827814720.
Happy Learning!

No comments:

Post a Comment