Table of Contents
Back Button
Back to Chapter
C++ Setting Up
C++ Introduction
No items found.
C++ Basics
No items found.
C++ Control Statements
No items found.
C++ Logical Operators
No items found.
C++ Procedural Programming
No items found.
C++ Structural Programming
No items found.
C++ Implementation of OOPS
No items found.
C++ Arrays and Vectors
No items found.
C++ Error Handling
No items found.
C++ File Handling
No items found.

C++ Pre-Defined Functions

Pre defined functions

These are Functions which are already defined. Any set of subroutines that perform standard mathematical functions included in a programming language; either included in a program at compilation time, or called when a program is executed is called a function.

The definitions of many common functions are found in the cmath and cstdlib libraries. To access these functions, your program must contain the directive(s) in addition to using namespace std;

Directives:- The preprocessors(like #include<iostream>) are the directives, which give instructions to the compiler to preprocess the information before the actual compilation starts. All preprocessor directives begin with #, and only white-space characters may appear before a preprocessor directive on a line.

The list has some commonly used pre-defined functions based on the libraries we need to include in order to use them.

Input and Output Functions

The C++ standard libraries provide an extensive set of input/output functions. In C++ I/O occurs in streams, which are sequences of bytes. If bytes flow from a device like a keyboard, a disk drive, or a network connection, etc. to the main memory, this is called an input operation and if bytes flow from main memory to a device like a display screen, a printer, a disk drive, or a network connection, etc., this is called output operation.

C++ language offers us console input/output functions. As the name suggests, the console input/output functions allow us to -

  • Read the input from the keyboard, entered by the user at the console.
  • Display the output to the user at the console.

Lets take a look at some of the important input and output functions is C++.

cout is an object of class ostream and cin is an object of the class istream.



A string in C++ is actually an object, which contains functions that can perform certain operations on the string. For example, you can concatenate strings with the append();  function.

#include using namespace std; main() { string firstName = "John "; string lastName = "Wick"; string fullName = firstName.append(lastName); cout << fullName; }
Output: John Wick

String length

To get the length of a string, use the length() function.

#include using namespace std; main() { string txt = "ABCXYZ"; cout << "The length of the text string is: " << txt.length(); }

Input Strings

Consider the following example where we take a string input from user using extraction operator.

#includue using namesapce std; main() { string fullname; cout << "whats your full first name: "; cin >> fullname; // get user input from the keyboard cout << "Your name is: " << fullname; }
Output: whats your full first name: John Snow Your name is: John

when working with strings, we often use the getline() function to read a line of text. It takes cin as the first parameter and the string variable as the second.


getline (cin, string);


#include using namespace std; main() { string fullName; cout << "Type your full name: "; getline (cin, fullName); cout << "Your name is: " << fullName; }

Mathematical functions

C++ has many functions that allows us to perform mathematical tasks on numbers.

The max function can be used to find maximum of two integers.


The min function is used to find smaller of the two integers.



#include using namespace std; main() { cout<
Output: 30 4

cmath header file:

Functions, such as sqrt (square root), round (rounds a number), and log (natural logarithm), can be found in the <cmath> header file.

#include using namespace std; main() { cout << sqrt(64); cout << round(2.6); cout << log(2); }
Output: 7 4 1.38629

In the table below you will find a few other mathematical functions available to us in the <cmath> header file.

Time and date

Suppose you want to retrieve the current system date and time, either as a local time or as a Coordinated Universal Time (UTC).

The C++ standard library does not provide a proper date type for time. C++ inherits the structs and functions for date and time manipulation from C. To access date and time-related functions and structures, you must include the<ctime> header file in your C++ program.

There are four time-related types offered by C++: clock_t , time_t and  size_t

We will be learning how to use  time_t. This returns the current calendar time of the system in the number of seconds elapsed since January 1, 1970. If the system has no time, .1 is returned. Basically, it will return the time in the system.


time_t time1(time_t *time1);//time1 is a variable of time_t type

Let us see how we can use this to print the current local time as well as the universal time UTC.

#include #include using namespace std; int main() { // current date/time based on current system time_t rightNow = time(0); // convert rightnow to string form char* date = ctime(&rightNow); cout << "The local date and time is: " << date << endl; // convert rightnow to tm struct for UTC tm *gtm = gmtime(&rightNow); date = asctime(gtm); cout << "The UTC date and time is: "<< date<< endl; }
Output: The local date and time is: Tue May 25 16:59:43 2021 The UTC date and time is:Tue May 25 11:29:43 2021
char *ctime(const time_t *time);

This returns a pointer to a string of the form day month year hours : minutes : seconds year.

char * asctime ( const struct tm * time );

This returns a pointer to a string that contains the information stored in the structure pointed to by time converted into the form: day month date hours : minutes : seconds year.

Ask queries
Contact Us on Whatsapp
Hi, How Can We Help You?