Inbuilt Function
Inbuilt Function
in
Inbuilt Function
List of Some Commonly used inbuilt Functions in C++
Standard input/output functions
stdio.h
gets (), puts ()
Character Functions
ctype.h
isalnum (), isalpha (),isdigit (), islower (),isupper (), tolower
(),toupper ()
String Functions
string.h
strcpy (), strcat (),strlen (), strcmp (),strcmpi (), strrev
(),strlen (), strupr (),strlwr ()
iomanip.h
setw(),
Mathematical Functions
math.h
abs (), pow (), sgrt (),sin (), cos (), abs ()
Other Functions
stdlib.h
randomize (), random (),itoa (), atoi ()
Using Library Functions
a b c d e f g h i j k l m n o p q r s t u v w x y z
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
o 1 2 3 4 5 6 7 8 9
Return Value
The function returns nonzero if c is alphanumeric otherwise this
will return zero which will be equivalent to false.
Example
#include <iostream.h>
#include<ctype.h>
int main() {
if( isalnum( '#' ) )
{
cout<< "Character # is not alphanumeric\n" ;
}
if( isalnum( 'A' ) )
{
cout<< "Character A is alphanumeric\n" ;
}
return 0;
}
It will produce following result:
Character A is alphanumeric
int isalpha(int c);
Description:
The function returns nonzero if c is any of:
a b c d e f g h i j k l m n o p q r s t u v w x y z
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Return Value
The function returns nonzero if c is just alpha otherwise this
will return zero which will be equivalent to false.
Example
#include <iostream.h>
#include<ctype.h>
int main() {
if( isalpha( '1' ) )
{
cout<< "Character 1 is not alpha\n" ;
}
http://www.kvsecontents.in/index.php/inbuilt-functions?tmpl=component&print=1&page= 2/14
9/13/2018 Inbuilt Function | KVSeContents.in
Example
#include <iostream.h>
#include<ctype.h>
int main() {
if( isdigit( '1' ) )
{
cout<< "Character 1 is a digit\n" ;
}
if( isdigit( 'A' ) )
{
cout<< "Character A is digit\n" ;
}
return 0;
}
It will produce following result:
Character 1 is a digit
int islower(int c);
Description:
The function returns nonzero if c is any of:
a b c d e f g h i j k l m n o p q r s t u v w x y z
Return Value
http://www.kvsecontents.in/index.php/inbuilt-functions?tmpl=component&print=1&page= 3/14
9/13/2018 Inbuilt Function | KVSeContents.in
Example
#include <iostream.h>
#include<ctype.h>
int main() {
cout<< "Upper case of t is \n"<<toupper('t');
return 0;
}
It will produce following result:
Upper case of t is T
C++ Library Functions Header File :string.h
char *strcpy (char *dest, char *src);
Description:
The strcpy function copies characters from src to dest including
the terminating null character.
Return Value
The strcpy function returns dest.
Example
#include <iostream.h>
http://www.kvsecontents.in/index.php/inbuilt-functions?tmpl=component&print=1&page= 5/14
9/13/2018 Inbuilt Function | KVSeContents.in
#include <string.h>
int main() {
char src[20];
char dest[20];
strcpy(src, "Hello");
strcpy(dest, src");
// Will copy the contents from src array to dest array
cout<<"dest ="<<dest;
return 0;
}
It will produce following result:
dest = Hello
char *strcat(char *dest, const char *src);
Description:
The strcat function concatenates or appends src to dest. All
characters from src are copied including the terminating null
character.
Return Value
The strcat function returns dest. Example
#include <string.h>
#include <iostream.h>
int main()
{
char src[20];
char dest[20];
strcpy(src, "Ayan");
strcpy(dest, "Sharma");
cout<<"Concatenated String = \n"<<strcat(src,dest);
return 0;
http://www.kvsecontents.in/index.php/inbuilt-functions?tmpl=component&print=1&page= 6/14
9/13/2018 Inbuilt Function | KVSeContents.in
}
It will produce following result:
Concatenated String = AyanSharma
int strlen(char *src );
Description:
The strlen function calculates the length, in bytes, of src. This
calculation does not include the null terminating character.
Example
#include <string.h>
#include <iostream.h>
int main()
{
char src[20];
strcpy(src, "Ambari");
cout<<"Length of string :"<<src<< " is "<<strlen(src );
return 0;
}
It will proiduce following result:
Length of string Ambari is 6
[email protected] < >'; document.write( '' );
document.write( addy_text77487 ); document.write( '<\/a>' ); //-->
;;
int main(void)
{
char *s = "abcde";
/* converts string to upper case characters */
cout<<endl<<s;
return 0;
}
Output
ABCDE
Declaration: char *strrev(char *s);
Description:
strrev changes all characters in a string to reverse order,
except the terminating null character.For example, it would change
AYAN\0
to
NAYA\0
#include<string.h>
#include<iostream.h>
int main(void)
{
char *s = "AYAN";
cout<<"After strrev(): "<<strrev(s);
return 0;
}
Output
NAYA
C++ Library Functions Header File :math.h
Declaration :- abs(int n);
Description:
abs is not a function but is a macro and is used for calculating
absolute value of a number.
#include <iostream.h>
#include <math.h>
http://www.kvsecontents.in/index.php/inbuilt-functions?tmpl=component&print=1&page= 9/14
9/13/2018 Inbuilt Function | KVSeContents.in
int main()
{
int n, result;
cout<<"Enter an integer to calculate it's absolute value\n";
cin>>n;
result = abs(n);
cout<<"Absolute value of "<<n<< "is "<<result;
return 0;
}
Output
Enter an integer to calculate it's absolute value -5
Absolute value of -5 is 5
Declaration :- double pow(double, double);
Description:
pow function returns x raise to the power y where x and y are
variables of double data type.
#include <iostream.h>
#include <math.h>
int main()
{
int m,n, result;
cout<<"Enter two integers to calculate power\n";
cin>>m>>n;
result = pow(m,n);
cout<<"Result is "<<result;
return 0;
}
Output
Enter two integers to calculate power 3 4
http://www.kvsecontents.in/index.php/inbuilt-functions?tmpl=component&print=1&page= 10/14
9/13/2018 Inbuilt Function | KVSeContents.in
Result is 81
Declaration :- double sqrt(double);
Description:
sqrt function returns square root of a number.
#include <iostream.h>
#include <math.h>
int main()
{
double n, result;
cout<<"Enter an integer to calculate it's square root \n";
cin>>n;
result = sqrt(n);
cout<<"Result is "<<result;
return 0;
}
Output
Enter an integer to calculate it's square root 9
Result is 3
Declaration: double sin(double);
Description:
Sin function returns sine of an angle(in radian).
#include <iostream.h>
#include <math.h>
int main()
{
double result, x = 0.5;
result = sin(x);
cout<<"sin(x ) of 0.5 is "<<result;
http://www.kvsecontents.in/index.php/inbuilt-functions?tmpl=component&print=1&page= 11/14
9/13/2018 Inbuilt Function | KVSeContents.in
return 0;
}
Output
sin(x) of 0.5 is 0.479426
Declaration: double cos(double);
Description:
Cos function returns cosine of an angle(in radian).
1 radian = 57.2958(approximately).
#include <iostream.h>
#include <math.h>
int main()
{
double result, x = 0.5;
result = cos(x);
cout<<"cos(x) of 0.5 is "<<result;
return 0;
}
Output
cos(x) of 0.5 is 0.877583
C++ Library Functions Header File :stdlib.h
Declaration: int random(int n);
Description:
random return a random number between 0 to (n-1)
#include <stdlib.h>
#include <iostream.h>
#include <time.h>
/* prints a random number in the range 0 to 9 */
int main(void)
http://www.kvsecontents.in/index.php/inbuilt-functions?tmpl=component&print=1&page= 12/14
9/13/2018 Inbuilt Function | KVSeContents.in
{
randomize();
cout<<"Random number from 0-9 range:\n"<<random(10);
return 0;
}
Declaration: void randomize(void);
Description:
randomize initializes random number generator
#include <stdlib.h>
#include <iostream.h>
#include <time.h>
/* prints 3 random number in the range 0 to 9 */
int main(void)
{
randomize();
cout<<"3 random numbers from 0-9 range:\n";
for(int i=0;i<3;i++)
cout<<random (10)<<endl;
return 0;
}
Declaration:
char*itoa(int value,char *string,int radix);
Description: itoa converts an integer to a string
#include <iostream.h>
#include <stdlib.h>
int main(void)
{
int number = 444;
char string[5];
http://www.kvsecontents.in/index.php/inbuilt-functions?tmpl=component&print=1&page= 13/14
9/13/2018 Inbuilt Function | KVSeContents.in
http://www.kvsecontents.in/index.php/inbuilt-functions?tmpl=component&print=1&page= 14/14