C++
Header Files and Built-in Functions
Header
files provide function prototype and definitions for library functions. Data
types and constants used with the library functions are also defined in them. A
header file can be identified by their file extension .h. A single header file may contain multiple built-in functions.
For
example: stdio.h is a header file
contains pre-defined “standard
input/output” functions.
This
header file defines the standard I/O predefined functions getchar(), putchar(), gets(),
puts() and etc.
The
predefined function getchar() is
used to get a single character from keyboard and putchar() function is used to display it.
#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
Function
gets() reads a string from standard input and stores it into the string pointed
by the variable. Function puts()
prints the string read by gets()
function in a newline.
#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);
}
Enter a string : Computer Science
You entered: Computer Science
This
header file defines various operations on characters. Following are the various
character functions available in C++. The header file ctype.h is to be included to use these functions in a program.
This
function is used to check whether a character is alphanumeric or not. This function returns non-zero value if c is a
digit or a letter, else it returns 0.
int isalnum (char c)
int r = isalnum(‘5’);
cout <<
isalnum('A') <<’\t’<<r;
But
the statements given below assign 0 to the variable n, since the given
character is neither an alphabet nor a digit.
char c = '$';
int n = isalnum(c);
cout<<c;
Output:
0
#include<iostream>
#include<stdio.h>
#include<ctype.h>
using namespace std;
int main()
{
char ch;
int r;
cout<<"\n
Type a Character :";
ch = getchar();
r = isalnum(ch);
cout<<"\nThe
Return Value of isalnum(ch) is :"<<r;
}
Type a Character :A
The Return Value of isalnum(ch) is :1
Type a Character :?
The Return Value of isalnum(ch) is :0
The
isalpha() function is used to check whether the given character is an alphabet
or not.
int isalpha(char c);
This
function will return 1 if the given character is an alphabet, and 0 otherwise
0. The following statement assigns 0 to the variable n, since the given
character is not an alphabet.
int n = isalpha(‘3’);
But,
the statement given below displays 1, since the given character is an alphabet.
cout << isalpha('a');
#include<iostream>
#include<stdio.h>
#include<ctype.h>
using namespace std;
int main()
{
char ch;
cout <<
"\n Enter a charater: ";
ch = getchar();
cout<<"\n
The Return Value of isalpha(ch) is :" << isalpha(ch) ;
}
Enter a charater: A
The Return Value of isalpha(ch) is :1
Enter a charater: 7
The Return Value of isalpha(ch) is :0
This
function is used to check whether a given character is a digit or not. This
function will return 1 if the given character is a digit, and 0 otherwise.
int isdigit(char c);
When
the following program is executed, the value of the variable n will be 1,since
the given character is not a digit.
using namespace std;
#include<iostream>
#include<ctype.h>
int main()
{
char ch;
cout <<
"\n Enter a Character: ";
cin >> ch;
cout<<"\n
The Return Value of isdigit(ch) is :" << isdigit(ch) ;
}
Enter a Character: 3
The Return Value of isdigit(ch) is :1
Enter a Character: A
The Return Value of isdigit(ch) is :0
*Return 0; (Not Compulsory in latest
compilers)
This
function is used to check whether a character is in lower case (small letter)
or not. This functions will return a non-zero value, if the given character is
a lower case alphabet, and 0 otherwise.
int islower(char c);
After
executing the following statements, the value of the variable n will be 1 since the given character is in lower case.
char ch = 'n';
int n = islower(ch);
But
the statement given below will assign 0 to the variable n, since the given
character is an uppercase alphabet.
int n = islower('P');
This
function is used to check the given character is uppercase. This function will
return 1 if true otherwise 0. For the following examples value 1 will be assigned to n and 0 for m.
int n=isupper(‘A’);
int m=isupper(‘a’);
This
function is used to convert the given character into its uppercase. This
function will return the upper case equivalent of the given character. If the
given character itself is in upper case, the output will be the same.
char toupper(char c);
The
following statement will assign the character constant 'K' to the variable c.
char c = toupper('k’);
But,
the output of the statement given below will be 'B' itself.
cout <<toupper('B');
This
function is used to convert the given character into its lowercase. This
function will return the lower case equivalent of the given character. If the
given character itself is in lower case, the output will be the same.
char tolower(char c);
The
following statement will assign the character constant 'k' to the variable c.
char c = tolower('K’);
But,
the output of the statement given below will be 'b' itself.
cout
<<tolower('b');
The
library string.h (also referred as
cstring) has several common functions for dealing with strings stored in arrays
of characters. The string.h header file to be included before using any string
function.
The
strcpy() function takes two
arguments: target and source. It copies the character string pointed by the
source to the memory location pointed by the target. The null terminating character
(\0) is also copied.
#include <string.h>
#include <iostream>
using namespace std;
int main()
{
char source[] =
"Computer Science";
char
target[20]="target";
cout<<"\n
String in Source Before Copied :"<<source;
cout<<"\n
String in Target Before Copied :"<<target; strcpy(target,source);
cout<<"\n
String in Target After strcpy function Executed :"<<target; return
0;
}
String in Source Before Copied :Computer Science
String in Target Before Copied :target
String in Target After strcpy function Executed :Computer
Science
The
strlen() takes a null terminated
byte string source as its argument and returns its length. The length does not
include the null(\0) character.
#include <string.h>
#include <iostream>
using namespace std;
int main()
{
char source[ ] =
"Computer Science";
cout<<"\nGiven
String is "<<source<<" its Length is
"<<strlen(source);
return 0;
}
Given String is Computer Science its Length is 16
The
strcmp() function takes two
arguments: string1 and string2. It compares the contents of string1 and string2
lexicographically.
The strcmp() function returns a:
•
Positive value if the first differing character in string1 is greater than the
corresponding character in string2. (ASCII values are compared)
•
Negative value if the first differing character in string1 is less than the
corresponding character in string2.
•
0 if string1 and string2 are equal.
#include <string.h>
#include <iostream>
using namespace std;
int main()
{
char string1[] =
"Computer";
char string2[] =
"Science";
int result;
result =
strcmp(string1,string2);
if(result==0)
{
cout<<"String1
: "<<string1<<" and String2 : "<<string2
<<"Are Equal";
}
if (result<0)
{
cout<<"String1
:"<<string1<<" and String2 : "<<string2
<<" Are Not Equal";
}
}
String1 : Computer and String2 : Science Are Not Equal
The
strcat() function takes two
arguments: target and source. This function appends copy of the character
string pointed by the source to the end of string pointed by the target.
#include <string.h>
#include <iostream>
using namespace std;
int main()
{
char target[50] =
"Learning C++ is fun";
char source[50] =
" , easy and Very useful";
strcat(target,
source);
cout << target
;
return 0;
}
Learning C++ is fun , easy and Very useful
The
strupr() function is used to convert
the given string into Uppercase letters.
using namespace std;
#include<iostream>
#include<ctype.h>
#include<string.h>
int main()
{
char str1[50];
cout<<"\nType
any string in Lower case :";
gets(str1);
cout<<"\n
Converted the Source string “<<str1<<into Upper Case is
"<<strupr(str1); return 0;
}
Type any string in Lower case : computer science
Converted the Source string computer science into Upper Case is
COMPUTER SCIENCE
The
strlwr() function is used to convert
the given string into Lowercase letters.
using namespace std;
#include<iostream>
#include<ctype.h>
#include<string.h>
int main()
{
char str1[50];
cout<<"\nType
any string in Upper case :";
gets(str1);
cout<<"\n
Converted the Source string “<<str1<<into Lower Case is
"<<strlwr(str1);
}
Type any string in Upper case : COMPUTER SCIENCE
Converted the Source string COMPUTER SCIENCE into lower Case is computer science
Most
of the mathematical functions are defined in math.h header file which includes
basic mathematical functions.
The
cos() function takes a single argument in radians. The cos() function returns
the value in the range of [-1, 1]. The returned value is either in double,
float, or long double.
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
double x = 0.5,
result;
result = cos(x);
cout <<
"COS("<<x<<")= "<<result;
}
COS(0.5)= 0.877583
The
sqrt() function returns the square
root of the given value of the argument. The sqrt() function takes a single non-negative argument. If a negative value is passed as an argument to sqrt() function, a domain
error occurs.
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
double x = 625,
result;
result = sqrt(x);
cout <<
"sqrt("<<x<<") = "<<result;
return 0;
}
sqrt(625) = 25
The
sin() function takes a single
argument in radians. The sin()
function returns the value in the range of [-1, 1]. The returned value is
either in double, float, or long double.
The
pow() function returns base raised
to the power of exponent. If any argument passed to pow() is long double, the return type is promoted to long double.
If not, the return type is double. The pow()
function takes two arguments:
base - the base value
exponent - exponent of the base
#include <iostream>
#include <math.h>
using namespace std;
int main ()
{
double base,
exponent, result;
base = 5;
exponent = 4;
result = pow(base,
exponent);
cout <<
"pow("<<base << "^" << exponent <<
") = " << result;
double x = 25;;
result = sin(x);
cout <<
"\nsin("<<x<<")= "<<result;
return 0;
}
pow(5^4) = 625
sin(25)= -0.132352
The
srand() function in C++ seeds the
pseudo random number generator used by the rand()
function. The seed for rand() function
is 1 by default. It means that if no
srand() is called before rand(),
the rand() function behaves as if it
was seeded with srand(1). The srand() function takes an unsigned
integer as its parameter which is used as seed by the rand() function. It is defined in<cstdlib>or <stdlib.h>header
file.
#include<iostream>
#include<cstdlib.h>
using namespace std;
int main()
{
int random = rand();
/* No srand() calls before rand(), so seed = 1*/
cout <<
"\nSeed = 1, Random number = " << random; srand(10);
/* Seed = 10 */
random = rand();
cout <<
"\n\nSeed = 10, Random number = " << random;
return 0;
}
Seed = 1, Random number = 41
Seed = 10, Random number = 71
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.