I wonder why atoi is performed without error without c++ cstdlib.

Asked 2 weeks ago, Updated 2 weeks ago, 2 views

I am solving the problem of changing the char* array to int.

I knew that atoi() was included in cstdlib, and

When I used the atoi in the professor's ppt, cstdlib was always included,

During this assignment, I observed that it ran well without errors even though I included only iostream without cstdlib.

Below is the corresponding code. I'm so curious why there's no error.

c++ atoi

2022-09-20 15:00

1 Answers

I'm enclosing other header files inside the iostream header file, but those other header files are enclosing another header file again. In the process, there was no error because one header file was enclosing cstdlib.

If I compile the code below, there will be an error. Uncommenting and compiling iostream should not cause errors.

//#include <iostream>
#include <cstdio>

using namespace std;

int main()
    printf("%d,", atoi("1234"));

In the long run, the internal implementation of header files may change, and the embedded header files may change, so when you use a particular function, you must accurately include the header files that contain that function.

2022-09-20 15:00

If you have any answers or tips

© 2022 pinfo. All rights reserved.