C++ Programming

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

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.

Amazon Purchase Links:

Top Gaming Computers

Best Laptops

Best Graphic Cards

Portable Hard Drives

Best Keyboards

Best High Quality PC Mic

Computer Accessories

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

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:

#include <stdio.h> 
void main()
{ 
char Name[10] = "Fawad Khan"; 
int Marks = 850;
 float Percentage = 80.95; 
char Grade = 'A';
 printf(" Name = %s", Name); 
printf("\n Marks = %d", Marks); 
printf("\n Percentage = %f", Percentage); 
printf("\n Grade = %c", Grade); 
}
Sample output :
Name = Fawad Khan 
Marks = 850 
Percentage = 80.95 
Grade = A

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:

#include <stdio.h>
 #include <conio.h>
 void main() 
{
int RNO = 101;
 float PER = 72.0;
 char GRADE=''A'; 
char S[10]; 
clrscr();
 sprintf(s,"%d%f%c",RNO, PER, GRADE);
printf("Value of $ - %s", S); 
getch(); 
}
OUTPUT
 Value of S 10172.000000A 

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:

#include<stdio.h>
#include<conio.h>
Void main()
{
clrscr();
gotoxy(40,12);
printf(“Electroniclinic”)
getch();
}

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:

#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
printf(“Fawad”);
printf(“\nkhan”);
printf(“\nElectronic clinic”);
gotoxy(1,2);
deline();
getch();

}
Output:
Fawad
Electronic clinic

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

#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
printf(“Fawad”);
printf(“\nkhan”);
gotoxy(1,2);
insline();
printf(“\nElectronic clinic”);

getch();

}
Output:
Fawad
Electronic clinic
Khan

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:

#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
lowvideo();
cprintf(“Fawad”);
highvideo();
cprintf(“Khan”);
normvideo();
cprintf(“\n\rElectronic clinic”);
getch();
}
Output:
Fawad
Khan
Electronic clinic

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:

#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
textbackground(WHITE);
textcolor(BLACK);
cprintf(“Fawad”)
textbackground(GREEN);
textcolor(YELLOW);
cprintf(“Khan”)

getch();

}

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.

Cout<<”Electronic”;

Cout<<”clinic”;

Output:

Electronicclinic

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.

cout<<”Fawad\n”;

cout<<”Khan\n”;

cout<<”Electroniclinic”;

output:

Fawad

Khan

Electroniclinic
cout<<”Fawad”;

cout<<”\nKhan”;

cout<<”\nElectroniclinic”;

output:

Fawad

Khan

Electroniclinic
cout<<”Fawad”;

cout<<”\nKhan\n”;

cout<<”\nElectroniclinic”;

output:

Fawad

Khan

Electroniclinic

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.

cout<<”Fawad”<<endl;

cout<<”Khan”<<endl;

cout<<”Electroniclinic”;

output:

Fawad

Khan

Electroniclinic
cout<<”Fawad”;

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

cout<< endl<<”Electroniclinic”;

output:

Fawad

Khan

Electroniclinic
cout<<”Fawad”;

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

cout<< ”Electroniclinic”;

output:

Fawad

Khan

Electroniclinic

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:

#include <iostream.h>
Void main()
{
Cout<<”Sum of  ”<<4<<”And”<<4<<”=”<<4+4;
}
Output:
Sum of 4 and 4 = 8

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

#include <iostream.h>
#include <conio.h>
#include <math.h>
void main()
{
clrscr();
cout<<"\tSQUARE TABLE"<<endl;
cout<<"\t***********"<<endl;
cout<<endl;
cout<<"*************************************<<endl;
cout<<"N\t\tN-SQUARED"<<endl;

cout<<"************************************"<<endl;
cout<<l<<"\t\t"<<pow(1, 2)<<endl;
out<<2<<"\t\t"<<pow (2, 2)<<endl;
cout<3<<"\t\t"<<pow (3,2)<<endl;
cout<<4<<"\t\t"<<pow (4,2) <<endl;.
cout<<5<<"\t\t"<<pow (5,2) <<endl;
cout<<"*************************************<<endl;
getch();
}
Output:
    SQUARE TABLE
   *******************
************************************
N           N-SQUARED
*************************************
1               1
2               4
3               9
4               16
5               25
************************************

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:

#include <iostream.h> 
#include <conio.h> 
void main() {
clrscr();
cout<<”sum of two integers :”<<6 +6<<endl;
cout<<”sum of two octal :”<<06 + 06<<”in decimal”<<endl;
cout<<”sum of two hexa :”<<0x06 +0x06<<”in decimal”<<endl;
cout<<”sum of two octal :”<<oct <<06 + 06<<”in octal”<<endl;
cout<<”sum of two hexa :”<<hexa<<0x06 +0x06<<”in hexa”<<endl;
cout<<”sum of two hexa :”<<dec<<0x06 +0x06<<”in decimal”<<endl;
getch();
 
}
Output:
Sum of two integers:12
Sum of two octal:12 in decimal
Sum of two hexa:12 in decimal
Sum of two octal:14 in octal
Sum of two hexa:c in hexa
Sum of two hexa :12 in decimal


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

#include<iostream.h>
#include<conio.h>
Void main()
{
clrscr();
cout.put(‘F’).put(‘A’).put(‘w’).put(‘A’).put(‘D’)<<endl;
cout.write(“fawad”, 1)<<endl;
cout.write(“fawad”, 3);
getch();

}
Output:
FAWAD
f
faw

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:

#include <iostream.h>
#include <conio.h >
void main()
{
clrscr();
cout.width(10);
cout.precision(2);
cout<<123.45678;
getch();
}

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:

#include<conio.h>
void main()
{
Char ch = ’*’;
putch(ch);
}

#include<conio.h>
void main()
{
putchar(‘*’);
}

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

#include<iostream.h>
#include<conio.h>
#include<math.h>
Void main()
{
int A,B,C;
float Root1, Root2;
A=1;
B=5;
C=2;
Root1=(-B+sqrt(B*B-4*A*C))/(2.0*A);
Root2=(-B-sqrt(B*B-4*A*C))/(2.0*A);
clrscr();
cout<<”1st Root = ”<<Root1<<endl;
cout<<”2nd Root”<<Root2<<endl;

}

 

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

Your email address will not be published. Required fields are marked *

Back to top button