Types of Inbuilt Functions in C++
×

Types of Inbuilt Functions in C++

112

Types of Inbuilt Functions in C++

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 x
  • pow(x, y) – Returns x raised to the power y
  • abs(x) – Returns the absolute value of x
  • ceil(x) – Rounds x up to the nearest integer
  • floor(x) – Rounds x down to the nearest integer
  • log(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 string
  • substr() – Extracts a substring
  • find() – Finds the position of a substring
  • append() – Adds content to the end of the string
  • compare() – 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 letter
  • isdigit(c) – Checks if c is a digit
  • islower(c) – Checks if c is a lowercase letter
  • isupper(c) – Checks if c is an uppercase letter
  • tolower(c) – Converts c to lowercase
  • toupper(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.


If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!

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!



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments

Coding Tag WhatsApp Chat