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

c++ standard libraries

Table of Contents

Introduction:

C++ standard libraries and how to use them– There are several C++ standard libraries to indicate different types of actions to be performed during execution. Output function are one of them. In this article we will discuss those functions which appear in almost every c++ program.



C++ standard libraries:

  1. Standard library printf() function:

One of the most common output function in c++ us the printf() function. The printf is used to display the data on default output device. It can be used to display messages values of variable and to print the answers of arithmetic expressions. It has the following general format.

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

The format string can contain simply a string and you know that a string is always enclosed in double quotes for example

printf(“Hello world ”);

the format string can contain format specification that begins with a  % sign followed by a single letter indicating the type of data to be inserted if more than one item is required to be print then these items must be separated with commas. Data type must be corresponded to the format specification otherwise you will get unpredictable result. Items can be variable constants expression and or function calls.

Consider the following examples in which %d is used as 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 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


  1. 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:

  1. 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

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



  1. 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 “programmingdigest” in the middle of the screen.

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

  1. C++ standard libraries delline() and insline function:

The delline function is used to delete the line at cursor position and move all line 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 following programming illustrate these two functions.

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


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

  1. C++ standard libraries 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 libraries highvideo(), lowvideo() and normvideo() function:


  1. 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 library textbackground() and textcolor() function:

  1. C++ standard library cout function:

Flow of data from one location to another location is known as 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 stand for console output.it is used to display messages values and values of variable and to print the answers of 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 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.

Cout<<”programming”;

Cout<<”digest”;

 

Output:

programmingdigest



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

cout<<”Fawad\n”;

cout<<”Khan\n”;

cout<<”programmingdigest”;

output:

Fawad

Khan

programmingdigest

 

cout<<”Fawad”;

cout<<”\nKhan”;

cout<<”\nprogrammingdigest”;

 

output:

Fawad

Khan

programmingdigest

 

cout<<”Fawad”;

cout<<”\nKhan\n”;

cout<<”\nprogrammingdigest”;

 

output:

Fawad

Khan

programmingdigest

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

          cout<<”Fawad\nKhan\nprogrammingdigest”;

“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.

cout<<”Fawad”<<endl;

cout<<”Khan”<<endl;

cout<<”programmingdigest”;

 

output:

Fawad

Khan

programmingdigest

 

cout<<”Fawad”;

cout<<”<<endl<<”Khan”;

cout<< endl<<”programmingdigest”;

 

output:

Fawad

Khan

programmingdigest

 

cout<<”Fawad”;

cout<<”<<endl<<”Khan”<<endl;

cout<< ”programmingdigest”;

output:

Fawad

Khan

programmingdigest


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

Cout<<”Fawad”<<endl<<”Khan”<<endl<<”programmingdigest”;

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:


  1. C++ standard library ‘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 libraries:

  1. 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():


  1. 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 are 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:

  1. 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 libraries putch(), and putchar() function:

The putch(ch) are 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().


  1.  library 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 puts() function automatically adds \n at the end of each string.


how to find the Quadratic Equation:

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

Programming:

 

 

Recommended For You

Fawad

About the Author: Fawad

My name is Shahzada Fawad and I am a Programmer. Currently, I am running my own YouTube channel "Expertstech", and managing this Website. My Hobbies are * Watching Movies * Music * Photography * Travelling * gaming and so on...

Leave a Reply

%d bloggers like this: