Standard Functions

  

Standard Functions

"standard functions" à pre-written functions àprovided by the C++ Standard Library.

a collection of classes and functionsà part of the C++ ISO standard.

perform common operations such as input/output, mathematical calculations, string manipulations, memory management, and more.

 

Key Aspects of Standard Functions:

C++ Standard Library: a comprehensive set of tools.

Headers: organised into headers. To use a particular functionà, include the corresponding

                 header file in the code using the #include directive.

Predefined:

Reusable: general-purpose.

Standardised: Defined by the C++ standard (e.g., ISO/IEC 14882).

 

Categories of Standard Functions

Some common categories of standard functions in C++:-

 

1. Mathematical Functions (<cmath>)

These functions perform mathematical operations.

  • sqrt(x): Returns the square root of x.
  • pow(base, exp): Returns base raised to the power of exp.
  • sin(x), cos(x), tan(x): Trigonometric functions (argument in radians).
  • abs(x): Returns the absolute value of x (for floating-point, use fabs(x)).
  • floor(x):Rounds down x to the nearest integer.

 

2. Input/Output Functions (<iostream>)

These handle console input and output.

  • cout: Not a function but an object used with << for output (e.g., cout << "Hello";).
  • cin: Not a function but an object used with >> for input (e.g., cin >> x;).
  • getline(cin, str): → Reads an entire line of input.

 

3. String Manipulation Functions (<cstring>)

Inherited from C, these operate on null-terminated character arrays.

  • strlen(str): Returns the length of string str.
  • strcmp(str1, str2): Compares two strings; returns 0 if equal.
  • strcpy(dest, src): Copies string src to dest.
  • strcat(str1, str2): Concatenates str2 to str1.

 

Character Handling Functions (<cctype>)

  • isalpha(c); → Checks if c is a letter.
  • isdigit(c); → Checks if c is a digit.
  • toupper(c); → Converts c to uppercase.
  • tolower(c); → Converts c to lowercase.

 

4. Memory Management Functions (<cstdlib>)

These deal with dynamic memory allocation (though C++ prefers new and delete).

  • malloc(size): Allocates memory of size bytes and returns a pointer.
  • free(ptr): Deallocates memory pointed to by ptr.
  • calloc(n, size): Allocates memory for n items of size bytes, initialised to zero.
  • new and delete → Operators for dynamic memory management.

 

5. Utility Functions (<cstdlib> and <ctime>)

Miscellaneous helper functions.

  • rand(): Generates a pseudo-random integer.
  • srand(seed): Seeds the random number generator.
  • time(nullptr): Returns the current time in seconds since epoch.

 

6. Type Conversion Functions (<cstdlib>)

Convert between data types.

  • atoi(str): Converts a string to an integer.
  • atof(str): Converts a string to a floating-point number.
  • exit(code); → Terminates the program immediately.
  • system("command"); → Executes a system command.

 

Example: Example Program Using Standard Functions

 

#include <iostream>  // For cout, cin

#include <cmath>     // For sqrt, pow, etc.

#include <cstring>   // For strlen, strcpy, etc.

 

int main() {

    double num = 16.0;

    std::cout << "Square root of " << num << " is " << sqrt(num) << std::endl;

 

    char str[] = "Hello";

    std::cout << "Length of '" << str << "' is " << strlen(str) << std::endl;

 

    return 0;

}

 

Output:

Square root of 16 is 4

Length of 'Hello' is 5

 

Advantages of Standard Functions

  • Efficiency: well-tested implementations.
  • Reusability: used across different parts of the program.
  • Portability: Available across different platforms and compilers.
  • Convenience: Save time and effort by providing common functionalities.

 

Drawbacks of Standard Functions

  • Learning Curve: extra knowledge required for beginners.
  • Overhead: unnecessary overhead if not used carefully.
  • Limited Control: Lack of control over the underlying implementation.

==============================================================

Post a Comment

0 Comments