C++ standard libraries and how to use them, C++ stl

(Last Updated On: November 16, 2020)

Table of Contents

Introduction:

C++ standard libraries and how to use them– There are a few C++ standard libraries to indicate various kinds of actions to be performed during execution. Output functions are one of them. In this article, we will discuss those functions which show up in pretty much every C++ program.

The printf() function:

One of the most common outputs function in C++ us the printf() function. The printf is utilized to display the information on the default output device. It very well may be utilized to display messages values of variable and to print the appropriate responses of arithmetic expressions. It has the accompanying general format.

printf(“the string which you print on screen ”,the data items which you want to print);

The format string can contain just string and you realize that a string is constantly enclosed in double-quotes for instance

printf(“Hello world ”);

the format string can contain format specification that starts with a % sign followed by a solitary letter demonstrating the type of data to be embedded in the event that more than one item is needed to be print, at that point these items must be separated with commas. Data type must be related to the format specification else you will get an eccentric result. Items can be variable constants expression or potentially function calls.

Consider the following examples in which %d is used as a format specification for integer data

printf(“%d”,8);  the output of this will be 8

printf(“%d%d%d”,1,2,3); the output of this will be 123

printf(“%d %d %d”,1,2,3); the output of this will be 1 2 3

printf(“%d\t%d\t%d\t”,1,2,3); the output of this will be 1        2       3

printf(“%d\n%d\n%d\n”,1,2,3); the output of this will be

1

2

3

The control string %d used in printf() is called conversion character begins with (% ) and ends with a conversion character (d) which indicates the type of the corresponding data item. printf() function makes formatting much easier because you put the formatting characters directly into the printf() statement.

A list of conversion character that can be used with printf() function is given below:

 

Data type

 

Description

 

Conversion character

Integer Short signed

Short unsigned

Long unsigned

Unsigned

Unsigned hexadecimal

Unsigned octal

d or I

u

Id

Iu

x

0

Real Float

Double

Exponential real

Either in f or e format depending on the value and specified precision

 

F

If

e

g

character Signed and unsigned c
String String s

 

The use of C++ standard library printf() function you must include the <stdio.h> header file in your programming. Consider the following programming example


Example how to print data using C++ standard library printf() function:

You can set the width of the output field for this by simply put a number (size of the field) between the % sign and the conversion character for example:

printf(“ %d”, 600)

the width specifier simply reserves the said number of columns on the screen for printing a value. In the above statement 6 columns will be reserved for the integer value of 600 which will not fill up the entire field as shown below;

6 0 0

 

C++ standard library sprint() function:

The sprint() function is used to write the output to an array of characters. sprint() function does not print the value of items on the screen rather it stores the value of the items (according to the format specifier) in the said character array. The following programming illustrates this function very clearly.

Example how to print data using C++ standard library sprintf() function:

C++ standard library clrscr() function:

The clrscr() function is used to clear the screen by filling it with the screen background color and locate the cursor in the upper left-hand corner of the screen this function has no returning value. For this function include<conio.h> header file in your programming

#include<conio.h>

Void main()

{

clrscr();

}

Like clrscr() function clreol() function is also available in C++ it is used to clear from the cursor to the end of the line.



C++ standard library gotoxy() function:

The gotoxy() function is used to position the cursor to the specified location (column, row) on the screen. It has the following format.

gotoxy(int X, int Y)

the gotoxy() function will be ignored by the compiler coordinators are supplied. For example, the following program will print the “Electroniclinic” in the middle of the screen.

Example how to print data on location using C++ standard library gotoxy() function:

C++ standard library delline() and insline function:

The delline function is used to delete the line at the cursor position and move all lines below it up to fill the gap. Similarly, insline() function is used to insert a blank line at the cursor position and all lines below are automatically moved down by one row. in the following programming illustrates these two functions.

Example how to use C++ standard library deline() function:

Example how to use C++ standard library insline() function:

C++ standard library highvideo() lowvideo() and normvideo() function:

The highvideo() selects high-intensity text characters lowvideo() selects low-intensity text characters and normvideo() selects normal-intensity text characters. These function work only with cprintf() function instead of printf().cprintf() function sends formatted output to the screen.


Example how to use C++ standard library highvideo(), lowvideo() and normvideo() function:

C++ standard library textbackground() and textcolor() function:

A new background color and a new foreground color can be selected by using textbackground() and textcolor() function respectively. These functions work with the function cprintf() instead of printf() the normvideo() function resets the foreground and background color.

Example how to use C++ standard libraries textbackground() and textcolor() function:

C++ standard library cout function:

Flow of data from one location to another location is known as a stream. The cout is the C++ standard library output stream that is used with the insertion operator (<<) to send data to the default output device. The cout stands for console output.it is used to display messages values and values of the variable and to print the answers of an arithmetic expression. For this stream include <iostream.h> header file in your programming. It has the following format.

Cout<<variable, constant or expression

For example:

cout<<”fawad khan”;

cout<<2+3*3%3;

it is important to note that after the execution of the cout statement the screen cursor will remain on the same line at the end of the last character that is printed. for example, consider the output of the two statements.

to move the cursor to the next line escape character  “\n“ is used for example the following three cout statements would produce the output on three separate lines.

the above three statements could be chained together in one single statement for the same output as above

cout<<”Fawad\nKhan\nElectroniclinic”;

“endl” (end line) is an output manipulator that does the same job as \n. it is used to end the line and move the cursor to the beginning of the next line.

like \n the statements could be chained together with help of endl in one single statement for the same output as above.

Cout<<”Fawad”<<endl<<”Khan”<<endl<<”Electroniclinic”;

Consider the following program example in which string constants character constant numeric constants and expression are chained together in one single statement with the help of insertion operators.


Example: how to find the sum of two number using  C++ standard library cout function:

Example how to find the square root using  C++ standard library cout and pow function:

C++ standard libraries ‘dec’,’oct’, and ’hex’ with cout manipulators:

The ‘dec’,’oct’, and ’hex’ manipulators are used to print the values in decimal octal and hexadecimal number system respectively

Example how to convert number in different number system using ‘dec’,’oct’, and ’hex’ C++ standard library:


C++ standard libraries put() and wirte() with cout function:

The function put() and write() can be used with cout in place of insertion operator (<<). The put() function is used to write a single character to the output device. Whereas the write() function is used to write the said number of characters on screen.

Example how to use C++ standard libraries put() and write():

C++ standard libraries width() and precision() and fill() with cout function:

The width() demarcates the width of the field for the said value. The precision() prints the floating number upto the prescribed decimal figure.in width() function unfilled location is filled with spaces whereas fill() function is called to fill up the unfilled locations with any given character.

Example how to use C++ standard libraries width(), precision() and fill() function:

output:

* * * * 1 2 3 . 4 6

C++ standard libraries putc(), putch() and putchar() character output functions:

The most fundamental character output function are putc(), putch() and putchar(). The putc() function is used to write a single character to a file whereas putch() and putchar() are used to write a single character to the screen. Note that <conio.h> header file is used for putch() whereas <stdio.h> header file is used for putchar() function.



Example how to use C++ standard library putch(), and putchar() function:

The putch(ch) is equivalent to printf(“%c”, ch) like printf() function it does not require special formatting programming can be made both smaller and quicker by using putch() or putchar().

C++ standard libraries puts() string output functions:

The puts() function write a string to the screen and append a newline character (\n) automatically after the string unlike printf(). Puts() can write only one string at a time.it has the following general form

For example:

puts(“Hello! World….”);

notice that \n has not been included in the string it isn’t needed since the puts() function automatically adds \n at the end of each string.


Example : if A=1,B=5, and C=2 write a program to fine out the square roots of quadratic equation using the following formula (-B±√(B^2-4AC))/2A

 

Recommended For You

About the Author: Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply