C++ standard libraries and how to use them, C++ stl
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:
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #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 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #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
1 2 3 4 5 6 7 8 9 | #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 “programmingdigest” in the middle of the screen.
Example how to print data on location using c++ standard library gotoxy() function:
1 2 3 4 5 6 7 8 9 | #include<stdio.h> #include<conio.h> Void main() { clrscr(); gotoxy(40,12); printf(“programmingdigest”) getch(); } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include <stdio.h> #include <conio.h> void main() { clrscr(); printf(“Fawad”); printf(“\nkhan”); printf(“\nprogrammingdigest”); gotoxy(1,2); deline(); getch(); } Output: Fawad programmingdigest |
Example how to use c++ standard library insline() function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include <stdio.h> #include <conio.h> void main() { clrscr(); printf(“Fawad”); printf(“\nkhan”); gotoxy(1,2); insline(); printf(“\nprogrammingdigest”); getch(); } Output: Fawad programmingdigest Khan |
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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include <stdio.h> #include <conio.h> void main() { clrscr(); lowvideo(); cprintf(“Fawad”); highvideo(); cprintf(“Khan”); normvideo(); cprintf(“\n\rprogrammingdigest”); getch(); } Output: Fawad Khan programmingdigest |
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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #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 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:
1 2 3 4 5 6 7 | #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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #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 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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #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():
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #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 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 2 3 4 5 6 7 8 9 10 11 12 13 | #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 libraries putch(), and putchar() function:
1 2 3 4 5 6 | #include<conio.h> void main() { Char ch = ’*’; putch(ch); } |
1 2 3 4 5 | #include<conio.h> void main() { putchar(‘*’); } |
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().
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #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; } |