Types of Inbuilt Functions in C++
0 112
Introduction to Inbuilt Functions in C++
C++ offers a wide range of inbuilt (predefined) functions provided by its Standard Library. These functions help developers perform common operations efficiently without having to write code from scratch. They are grouped under different categories like mathematical operations, string handling, character analysis, and more.
1. Mathematical Functions
The <cmath>
header includes many functions to perform mathematical operations:
sqrt(x)
– Returns the square root of xpow(x, y)
– Returns x raised to the power yabs(x)
– Returns the absolute value of xceil(x)
– Rounds x up to the nearest integerfloor(x)
– Rounds x down to the nearest integerlog(x)
– Returns the natural logarithm (base e) of x
Example:
#include <iostream> #include <cmath> using namespace std; int main() { cout << "Square root of 16: " << sqrt(16) << endl; cout << "Power of 2^5: " << pow(2, 5) << endl; return 0; }
2. String Functions
String operations in C++ are made easy using the <string>
header:
length()
– Returns the length of a stringsubstr()
– Extracts a substringfind()
– Finds the position of a substringappend()
– Adds content to the end of the stringcompare()
– Compares two strings
Example:
#include <iostream> #include <string> using namespace std; int main() { string str = "GeeksforGeeks"; cout << "Length: " << str.length() << endl; cout << "Substring: " << str.substr(5, 3) << endl; return 0; }
3. Character Functions
The <cctype>
header provides functions to test and manipulate character data:
isalpha(c)
– Checks if c is a letterisdigit(c)
– Checks if c is a digitislower(c)
– Checks if c is a lowercase letterisupper(c)
– Checks if c is an uppercase lettertolower(c)
– Converts c to lowercasetoupper(c)
– Converts c to uppercase
Example:
#include <iostream> #include <cctype> using namespace std; int main() { char ch = 'A'; cout << "Lowercase of A: " << char(tolower(ch)) << endl; cout << "Is '7' a digit? " << isdigit('7') << endl; return 0; }
4. Input/Output Functions
C++ uses cin
and cout
for input and output, defined in the <iostream>
header. These are the most commonly used inbuilt functions.
Example:
#include <iostream> using namespace std; int main() { int num; cout << "Enter a number: "; cin >> num; cout << "You entered: " << num << endl; return 0; }
Conclusion
Inbuilt functions in C++ offer pre-written logic to handle various programming needs—from math and strings to characters and input/output. Leveraging these functions enhances productivity, reduces coding errors, and makes programs more efficient.
For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!

Share:
Comments
Waiting for your comments