C++ Standard Library <cstdlib>

<cstdlib>Is a header file in the C++ standard library, providing various general utility functions, including memory allocation, process control, environment queries, sorting and searching, mathematical conversions, pseudo-random number generation, etc. These functions originally come from the C standard library.<stdlib.h>, standardized and extended in C++.

Syntax

In C++ programs, to usecstdlibfunctions in it, you need to include this header file first:

#include <cstdlib>

Common Functions

cstdlibcontains many useful functions. The following are some commonly used functions and their brief descriptions:

  1. exit(int status): terminates program execution and returns a status code.
  2. system(const char* command): Execute a command-line string.
  3. malloc(size_t size): Allocate memory of the specified size.
  4. free(void* ptr): Release previously allocated memory.
  5. atoi(const char* str): Convert a string to an integer.
  6. atof(const char* str): Convert a string to a floating-point number.
  7. rand(): Generate a random number.
  8. srand(unsigned int seed): Set the seed for the random number generator.

Example

The following are some examples of usingcstdlibfunctions in:

Example 1: UsingexitFunction

Example

#include <iostream>
#include <cstdlib>

int main() {
    std::cout << "This program will exit now." << std::endl;
    exit(0); // Exit the program normally
    return 0; // This line of code will not be executed
}

Output result:

This program will exit now.

Example 2: UsingsystemFunction

Example

#include <iostream>
#include <cstdlib>

int main() {
    std::cout << "Executing a system command: dir" << std::endl;
    system("dir"); // Display files and folders in the current directory on Windows
    return 0;
}

Output result:

Executing a system command: dir

Then display the list of files and folders in the current directory.

Example 3: UsingmallocandfreeFunction

Example

#include <iostream>
#include <cstdlib>

int main() {
    int* ptr = (int*)malloc(10 * sizeof(int)); // Allocate memory
    if (ptr == NULL) {
        std::cout << "Memory allocation failed." << std::endl;
        return 1;
    }

    for (int i = 0; i < 10; ++i) {
        ptr[i] = i * i; // Use allocated memory
    }

    for (int i = 0; i < 10; ++i) {
        std::cout << "Element " << i << ": " << ptr[i] << std::endl;
    }

    free(ptr); // release memory
    return 0;
}

Output result:

Element 0: 0
Element 1: 1
Element 2: 4
...
Element 9: 81

Example 4: UsingatoiandatofFunction

Example

#include <iostream>
#include <cstdlib>

int main() {
    std::string str1 = "123";
    std::string str2 = "456.78";

    int num1 = std::atoi(str1.c_str()); // Convert string to integer
    double num2 = std::atof(str2.c_str()); // Convert string to floating point number

    std::cout << "Integer: " << num1 << std::endl;
    std::cout << "Float: " << num2 << std::endl;
    return 0;
}

Output result:

Integer: 123
Float: 456.78

Example 5: UsingrandandsrandFunction

Example

#include <cstdlib>
#include <iostream>
#include <ctime>

int main() {
    std::srand(std::time(nullptr));  // Use current time as random number seed
    for (int i = 0; i < 5; ++i) {
        std::cout << std::rand() % 100 << " ";  // Generate a random number between 0 and 99
    }
    std::cout << std::endl;

    return 0;
}

Output result:

53 22 62 68 62 
other extensions