You are on page 1of 24

BUILT-IN FUNCTIONS IN C++ LANGUAGE:

The header file ‘ iomanip.h’ :

1) ‘setw’ Manipulator
The word ‘setw’ stands for set width the set width manipulator is used to display the
valure of an expression in specified columns. The value of the expression could be string
or number.

Syntax:
Set(w)
The n indicates the number of columns in which the value is to be displayed it is part of
iomanip.h

Example:

#include <iostream>
#include <iomanip>
using namespace std;

int main () {
cout << setw(10);
cout << 24 << endl;
return 0;
}

Output:

24

2) ‘setprecision’ Manipulato
The ‘setprecison’ manipulator is used to set the number of digits to be displayed after decimal
point.
Syntax:
setprecision(n)
The n displayed the number of digits displayed after the decimal points it can be unsigned,
positive integer constant variable or expression.

Example:
#include <iostream>
#include <iomanip>
using namespace std;

int main () {
double f =3.14159;
cout << setprecision(5) << f << '\n';
cout << setprecision(9) << f << '\n';
cout << fixed;
cout << setprecision(5) << f << '\n';
cout << setprecision(9) << f << '\n';
return 0;
}

Output:
3.1416
3.14159
3.14159
3.141590000

3) ‘fixed’ Manipulator:
‘fixed’ manipulator is used to further control the output of the floating number . it displays the
floating numbers in a fixed decimal point.

Syntax:
Cout<<fixed;
Example:
#include<iostream.h>
#include<iomanip.h>
int main()
{

Initializing the float values


double x = 1.23;

cout.precision(5);

cout << "without fixed flag: "


<< x << endl;

// Using fixed()
cout << "with fixed flag: "
<< fixed << x << endl;

return 0;
}

Output:
without fixed flag: 1.23
with fixed flag: 1.2300

4) ‘setfill’ Manipulator:
The ‘setfill’ manipulator is used to replace the leading or trailing blanks in the output with the
specified characters.

Syntax:
setfill (char_type c);

Example:
#include <iostream>
#include <iomanip>
using namespace std;
int main () {
cout << setfill ('x') << setw (10);
cout << 24 << endl;
return 0;
}

Output:
xxxxxxxx24

THE HEADER FILE “conio.h”:


The word conio stands for console input/output. The header file conio.h contains the built-in
functions that are used for input and output. Some important functions defined in this header file
are as follow:

5) CLRSCR():
The word “clrscr” stands for clear screen. The function is used to clear contents of the screen.
When this function is executed, the screen is cleared and the cursor move to the start of the first
line of the program.

Syntax:
clrscr();

Example:
#include<conio.h>
void main(){
clrscr();
}

Output:

6) clreol():
The word clreol stands for clear end of line. The function is used to clear current line from
current cursor position to the end of line. The remaining text on screen is not cleared.
Syntax:
clreol();

Example:
#include<conio.h>
#include<iostream.h>
void main()
{
std::cout<<”Hello world \r”;
clreol();
std::cout<<”Pakistan”;
}

Output:

Pakistan

7) getch();
The word getch stand for get character. The function is used to input character from the
keyboard. It inputs a single character. The character typed by the user does not appear on the
screen. It can be stored in a variable by using assignment statement. The user does not need
to press enter to compile the input. The function is frequently used to pause program
execution.

Syntax:
getch();

Example:
#include<conio.h>
#include<iostream.h>
void main()
{
char ch;
cout<<”Enter any character”;
ch=getch();
cout<<”you entered”<<ch;
getch();
}
Output:
You entered: a

8) getche():
the word getche stands for get character echo. The function is used to input single character
from the keyboard. The character types by the user also appeared on the screen. It can be
stored in a variable by using assignment statement. The user does not need to press enter to
compile the input.

Syntax:
getche();

Example:
#include<iostream>

using namespace std;

int main()

char ch;

cout<<“Want to continue (Press: (y/n)) : ”;

ch = getche();

return 0;

}
Output:

Want to continue (Press: (y/n)) :

9) kbhit():
The word kbhit stands for keyboard hit. The function is used to check if the user hit the keyboard
or not.it returns a non-zero value if the user presses any key on keyboard returns zero otherwise.
The value pf key can also be stored in a variable by using assignment operator.

Syntax:
Variable=kbhit();
The use of variable is optinal.

Example:
#include<conio.h>
#include<iostream.h>
void main()
{
while(kbhit())
cout<<”Pakistan”;
}

Output:
Pakistan

10) gotoxy():
the gotoxy function is used to move the cursor to a specified location on the screen. It moves
the cursor w.r.t to x-axis and y-axis. It is very useful when the output is to be displayed at a
specified location on the screen.

Syntax:
gotoxy(x,y);
the variable x indicates x-axis. Its value can be from 0 to 79. The y indicates y-axis. Its value
from 0-24.

Example:
#include<conio.h>
#include<iostream.h>
void main()
{
clrscr();
gotoxy(0,0);
cout<<”Pakistan Zindabad”;
gotoxy(1,50);
cout<<”Pakistan Zindabad”;
getch();
}

Output:
THE HEADER FILE “stdio.h”:
11) getchar():
The predefined function getchar() is used to get a single character from keyboard.

Syntax:
Variable=getchar();

Example:
#include<iostream>

#include<stdio.h>

using namespace std;

int main()

cout<<"\n Type a Character : ";

char ch = getchar();

cout << "\n The entered Character is: ";

putchar(ch);

return 0;

Output:
Type a Character : T

The entered Character is: T


12) putchar();
putchar() function is used to display a single character on screem.

Syntax:
Variable=putcgae();

Example:
#include<iostream>

#include<stdio.h>

using namespace std;

int main()

cout<<"\n Type a Character : ";

char ch = getchar();

cout << "\n The entered Character is: ";

putchar(ch);

return 0;

Output:
Type a Character : T
The entered Character is: T

13) gets():
Function gets() reads a string from standard input and stores it into the string pointed by the
variable.
Syntax:
gets(variable);

14) puts():
Function puts() prints the string read by gets() function in a newline.
Syntax:
puts(variable);

Example of gets and puts:


#include<iostream>

#include<stdio.h>

using namespace std;

int main()

char str[50];

cout<<"Enter a string : ";

gets(str);

cout<<"You entered: "

puts(str);

return(0);

}
Output:
Enter a string : Computer Science
You entered: Computer Science
You entered: Computer Science

The header file math.h :


15) the abs:
the abs() function is used to find the absolute value of an integer

Syntax:
Abs(x)

Example:
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
int x= -9;
std::cout << "Value of x is :" <<x<< std::endl;
cout<<"Absolute value of x is : "<<abs(x);

return 0;
}

Output:
Value of x is :-9
Absolute value of x is : 9

16) fabs:
the fabs() function is used to find the absolute value of floating point number

Syntax:
Fabs(x)

Example:
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
float x=11.2;
std::cout << "Value of x is :" <<x<< std::endl;
cout<<"Absolute value of x is : "<<fabs(x);
return 0;
}

Output:
Value of x is :11.2
Absolute value of x is : 11.2

17) ceil:
the ceil() function rounds a float or double value to next integer and returns it.

Syntax:
Ceil(x)

Example:
#include <iostream>
The rounde
using namespace std;
int main()
{
float x=-2.2;
std::cout << "Initial value of x is :"<<x;
cout<<'\n';
cout<<"final value of x is :"<<ceil(x);
return 0;

Output:
Initial value of x is :9.2
final value of x is :10

18) floor():
the floor() function round a float or double value to an integer and returns it. The rounded
value is not greater than original value

Syntax:
Floor(x)

Example:
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
float x=7.8;
std::cout << "Initial value of x is : " << x<<std::endl;
cout<<"Now, the value of x is :"<<floor(x);
return 0;

Output:
Initial value of x is : 7.8
Now, the value of x is :7

19) cos():
this function is used to find the trigonometric cosine of the specified angle

Syntax:
Cos(x)

Example:
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
double degree=60;
double d=60*3.14/180;
cout<<"Cosine of an angle is : "<<cos(d);
return 0;

Output:
Cosine of an angle is : 0.50046

20) sin():
the sin function is use to find trigonometric sine of the specified angle

Syntax:
Sin(x)

Example:
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
double degree=60;
double radian=degree*3.14/180;
cout<<"Sine of an angle is : "<<sin(radian);
return 0;
}

Output:
Sine of an angle is : 0.86576

21) tan():
The tan() function is used to the trigonometric tangent of the specified angle
Syntax:
Tan(x)

Example:
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
float degree=10;
float radian=degree*3.14/180;
cout<<"Tangent of an angle is : "<<tan(radian);
return 0;
}

Output:
Tangent of an angle is : 0.176236
22) log():
The log() function is used to find base 10logarithm of a given floating point value

Syntax:
Log(x)

Example:
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
int x=1;
std::cout << "Value of x is : " <<x <<std::endl;
cout<<"Log value of x is : "<<log(x);
return 0;
}

Output:
Value of x is : 1
Log value of x is : 0

23) log10():
thelog10() function is used to find the base 10 logarithm of a given floating point value

Syntax:
Log10(x)

Example:
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
int x=5;
std::cout << "Value of x is : " <<x <<std::endl;
cout<<"Log value of x is : "<<log10(x);
return 0;
}

Output:
Value of x is : 5
Log value of x is : 0.69897

24) fmod():
the fmod() function is used to find reminder by dividing two floating point value

Syntax:
Fmode(x,y)

Example:
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
double n=4.2;
double d=7.8;
std::cout << "The values of numerator and denominator are :" <<n<<" , "<< d<< std::endl;
std::cout << "fmod of these values is :"<<fmod(n,d) <<std::endl;
return 0; )
}

Output:
The values of numerator and denominator are :4.2 , 7.8
fmod of these values is :4.2

25) pow():
this function is used to find the result of one integer raised to power of second integer(nm)

Syntax:
Pow(x,y)

Example:
#include <iostream>
#include<cmath>
using namespace std;
int main()
{
int base=4;
int exponent=2;
int power=pow(base,exponent);
std::cout << "Power of a given number is :" <<power;
return 0;
}

Output:
Power of a given number is :16

26) sqrt():
The sqrt() function is used to calculate the square root of the floating point number.

Syntax:
sqrt();

Example:
#include <iostream>
#include<cmath>
using namespace std;
int main()
{ int arg=16;
std::cout << "Square root of a number is :" <<sqrt(arg);
return 0;
}

Output:
Square root of a number is :

The string.h header file

27) strlen():
this function is used to find the length of the function.

Syntax:
strlen();

Example:
#include<stdio.h>
#include <string.h>
int main(){
char ch[20]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};
printf("Length of string is: %d",strlen(ch));
return 0;
}

Output:
Length of string is: 10
28) strcat():
This function is used to Appends one string at the end of another.

Syntax:
strcat(string1, string2);

Example:
#include <stdio.h>
#include <string.h>
int main() {
char str1[100] = "This is ", str2[] = "programiz.com";

// concatenates str1 and str2


// the resultant string is stored in str1.
strcat(str1, str2);
puts(str1);
puts(str2);

return 0;
}

Output

This is programiz.com

programiz.com

29) strcpy():
this function is use to Copies a string into another.

Syntax:
char* strcpy(char* dest, const char* src);

Example:
#include <stdio.h>
#include <string.h>

int main() {
char str1[20] = "C programming";
char str2[20];

// copying str1 to str2


strcpy(str2, str1);

puts(str2); // C programming

return 0;
getch();
}

Output
C programming

30) strcmp():
this function is use to Compares two strings

Syntax:
int strcmp (const char* str1, const char* str2);

Example:
#include <stdio.h>
#include <string.h>

int main() {
char str1[] = "abcd", str2[] = "abCd", str3[] = "abcd";
int result;

// comparing strings str1 and str2


result = strcmp(str1, str2);
printf("strcmp(str1, str2) = %d\n", result);

// comparing strings str1 and str3


result = strcmp(str1, str3);
printf("strcmp(str1, str3) = %d\n", result);

return 0;
}
Output:

strcmp(str1, str2) = 1

strcmp(str1, str3) = 0

31) strspn():
strspn(s1, s2) returns the length of the initial part of the string s1 only containing the
characters of the string s2.

Syntax:

Strspn(s1,s2)

Example:
#include <stdio.h>

#include <string.h>

int main()

char s1[ ]= "123abc";

char s2[ ] = "123456790";

int i = strspn(s1, s2);

printf("%d\n", i);

return 0;
}
Output:
3

32) strpbrk:
strpbrk(s1, s2) returns the first occurrence of any of the characters of the string s2 in the
string s1.
Syntax:
Strpbrk(s1,s2)

Example:
#include <stdio.h>
#include <string.h>
int main()
{
char s1[ ]= "Hey 123 Blogsdope";
char s2[ ] = "123456790";
char *p;
p = strpbrk(s1, s2);
printf("%s\n", p);
return 0;
}

Output:

123 Blogsdope

33) strtok:
strtok(s1, s2) finds s2 in s1 and returns a pointer to it and returns NULL if not found.

Syntax:

Strtok(s1,s2)

Example:
#include <stdio.h>
#include <string.h>
int main()
{
char s1[ ]= "Hey,123,Blogsdope";
char *p;
p = strtok(s1, ",");

while(p != NULL)
{
printf("%s\n", p);
p = strtok(NULL, ",");
}
return 0;
}
Output:
Hey

123

Blogsdope

You might also like