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.
To get the length of a string, use the length() function.
Consider the following example where we take a string input from user using extraction operator.
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.
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.
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.
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.
Let us see how we can use this to print the current local time as well as the universal time UTC.
This returns a pointer to a string of the form day month year hours : minutes : seconds year.
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.