19

I'm using EXIT_FAILURE macro, so I need to include stdlib.h or cstdlib. But I don't know what the difference is. Should I use cXXX style header file rather than XXX.h?

Thank you.

Brian
  • 1,663
  • 5
  • 18
  • 26
  • Possible duplicate of [What's the difference between cstdlib and stdlib.h?](http://stackoverflow.com/questions/2900785/whats-the-difference-between-cstdlib-and-stdlib-h) – phuclv Mar 25 '17 at 04:09
  • Check out my updated answer here. http://stackoverflow.com/questions/13889467/should-i-include-xxxx-h-or-cxxxx-in-c-programs/43016708#43016708 – Santiago Varela Mar 25 '17 at 15:37

2 Answers2

29

<cstdlib> is just <stdlib.h> wrapped in the std namespace. You should use <cstdlib> in order to be keep your global namespace clean.

CharlesB
  • 86,532
  • 28
  • 194
  • 218
17

As EXIT_FAILURE is a macro, it makes no difference which you include. The cstdlib version will put the names of all the functions into the std namespace, so you can say things like:

std::exit(0);

but as macros don't respect namespaces, you can't say:

std::EXIT_FAILURE
  • Thanks, but I can still call `exit(EXIT_FAILURE)` without `std::` when I include `cstdlib`. – Brian May 17 '10 at 09:48
  • @Brian Yes, the exit function will be in both the global and the std namespaces, –  May 17 '10 at 09:50
  • Also, some overloads are declared to deal with [constness problem of strtol()](http://www.cpptalk.net/strtol-const-ness-problem-vt54984.html) and similar issues. – Alex Cohn Mar 03 '13 at 11:23