What's the difference between cstdlib and stdlib.h?
The first one is a C++ header and the second is a C header. Since the first uses a namespace, that would seem to be preferable.
What's the main difference between stdlib.h and cstdlib in C++?
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
difference between header files “stdio.h” and “stdlib.h”
One has set of function declarations, constants, macros and type definitions, the other has a different set of function declarations, constants, macros and type definitions. You are able to open the file with a simple text editor if you want to see the contents.
stdio
stands for standard input/output, its contents are usually but not exclusively related to input output operations, either with hardware or physical devices or with files supported by the system. A list of functions declarations, macros and type definitions available can be found here:
Functions:
Operations on files:
remove - Remove file
rename - Rename file
tmpfile - Open a temporary file
tmpnam - Generate temporary filename
File access:
fclose - Close file
fflush - Flush stream
fopen - Open file
freopen - Reopen stream with different file or mode
setbuf - Set stream buffer
setvbuf - Change stream buffering
Formatted input/output:
fprintf - Write formatted data to stream
fscanf - Read formatted data from stream
printf - Print formatted data to stdout
scanf - Read formatted data from stdin
snprintf - Write formatted output to sized buffer
sprintf - Write formatted data to string
sscanf - Read formatted data from string
vfprintf - Write formatted data from variable argument list to stream
vfscanf - Read formatted data from stream into variable argument list
vprintf - Print formatted data from variable argument list to stdout
vscanf - Read formatted data into variable argument list
vsnprintf - Write formatted data from variable argument list to sized buffer
vsprintf - Write formatted data from variable argument list to string
vsscanf - Read formatted data from string into variable argument list
Character input/output:
fgetc - Get character from stream
fgets - Get string from stream
fputc - Write character to stream
fputs - Write string to stream
getc - Get character from stream
getchar - Get character from stdin
gets - Get string from stdin
putc - Write character to stream
putchar - Write character to stdout
puts - Write string to stdout
ungetc - Unget character from stream
Direct input/output:
fread - Read block of data from stream
fwrite - Write block of data to stream
File positioning:
fgetpos - Get current position in stream
fseek - Reposition stream position indicator
fsetpos - Set position indicator of stream
ftell - Get current position in stream
rewind - Set position of stream to the beginning
Error-handling:
clearerr - Clear error indicators
feof - Check end-of-file indicator
ferror - Check error indicator
perror - Print error message
Macros:
BUFSIZ - Buffer size
EOF - End-of-File
FILENAME_MAX - Maximum length of file names
FOPEN_MAX - Potential limit of simultaneous open streams
L_tmpnam - Minimum length for temporary file name
NULL - Null pointer (macro )
TMP_MAX - Number of temporary files
Additionally: _IOFBF, _IOLBF, _IONBF (used with setvbuf)
and SEEK_CUR, SEEK_END and SEEK_SET (used with fseek).
Types:
FILE - Object containing information to control a stream
fpos_t - Object containing information to specify a position within a file
size_t - Unsigned integral type
stdlib
stands for standard library and it has general purpose functions, including dynamic memory management, random number generation, communication, arithmetics, searching, sorting and converting, etc. A list of functions declarations, macros and type definitions available can be found here:
Functions
String conversion
atof - Convert string to double
atoi - Convert string to integer
atol - Convert string to long integer
atoll - Convert string to long long integer
strtod - Convert string to double
strtof - Convert string to float
strtol - Convert string to long integer
strtold - Convert string to long double
strtoll - Convert string to long long integer
strtoul - Convert string to unsigned long integer
strtoull - Convert string to unsigned long long integer
Pseudo-random sequence generation
rand - Generate random number
srand - Initialize random number generator
Dynamic memory management
calloc - Allocate and zero-initialize array
free - Deallocate memory block
malloc - Allocate memory block
realloc - Reallocate memory block
Environment
abort - Abort current process
atexit - Set function to be executed on exit
at_quick_exit - Set function to be executed on quick exit
exit - Terminate calling process
getenv - Get environment string
quick_exit - Terminate calling process quick
system - Execute system command
_Exit - Terminate calling process
Searching and sorting
bsearch - Binary search in array
qsort - Sort elements of array
Integer arithmetics
abs - Absolute value
div - Integral division
labs - Absolute value
ldiv - Integral division
llabs - Absolute value
lldiv - Integral division
Multibyte characters
mblen - Get length of multibyte character
mbtowc - Convert multibyte sequence to wide character
wctomb - Convert wide character to multibyte sequence
Multibyte strings
mbstowcs - Convert multibyte string to wide-character string
wcstombs - Convert wide-character string to multibyte string
Macro constants
EXIT_FAILURE - Failure termination code
EXIT_SUCCESS - Success termination code
MB_CUR_MAX - Maximum size of multibyte characters
NULL - Null pointer
RAND_MAX - Maximum value returned by rand
Types
div_t - Structure returned by div
ldiv_t - Structure returned by ldiv
lldiv_t - Structure returned by lldiv
size_t - Unsigned integral type
Updating legacy C code headers to C++ stdlib
Your code may change in very subtle ways, due to the fact that the C++ standard headers use overloading where C used different names. This is most likely to cause trouble with cmath
.
stdlib.h
isn't going anywhere, so feel free to keep using it.
For example, compare:
#include <iostream>
using namespace std;
#include <stdlib.h>
#include <math.h>
int main(void)
{
double x = -2;
cout << (3/abs(x)) << endl;
return 0;
}
The results before and after switching to C++ headers are very different, even though the exact same C++ compiler and options are used in both cases.
When using C headers in C++, should we use functions from std:: or the global namespace?
From the C++11 Standard (emphasis mine):
D.5 C standard library headers [depr.c.headers]
- For compatibility with the C standard library ...
- Every C header, each of which has a name of the form name.h, behaves as if each name placed in the standard
library namespace by the corresponding cname header is placed within the global namespace scope. It is unspecified whether these names are first declared or defined within namespace scope (3.3.6) of the namespace std and are then injected into the global namespace scope by explicit using-declarations (7.3.3).- Example: The header
<cstdlib>
assuredly provides its declarations and definitions within the namespace
std
. It may also provide these names within the global namespace. The header<stdlib.h>
assuredly provides the same declarations and definitions within the global namespace, much as in the C Standard. It
may also provide these names within the namespacestd
.
Using the «name.h» headers is deprecated, they have been identified as candidates for removal from future revisions.
So, I would suggest to include the «cname» headers and to use the declarations and definitions from the std
namespace.
If you have to use the «name.h» headers for some reasons (it's deprecated, see above), I would suggest to use the declarations and definitions from the global namespace.
In other words: prefer
#include <cstdio>
int main() {
std::printf("Hello world\n");
}
over
#include <stdio.h>
int main() {
printf("Hello world\n");
}
Generating the partitions of a number
It's called Partitions. [Also see Wikipedia: Partition (number theory).]
The number of partitions p(n) grows exponentially, so anything you do to generate all partitions will necessarily have to take exponential time.
That said, you can do better than what your code does. See this, or its updated version in Python Algorithms and Data Structures by David Eppstein.
Read files with stdlib.h C++
FILE
is an opaque type; the C standard doesn't define its fields, so they can vary between implementations. Your program expects the FILE
type to have fields with certain names, but your system doesn't actually define it that way.
Don't rely on FILE
having specific fields, and don't dereference a FILE*
to try to access those fields. Instead, use the functions provided by the C standard library, such as fread
to read contents and ftell
to get the current position. These functions take a FILE*
parameter and access its contents in whatever way is correct for the system where the program is running.
Related Topics
Problems with Scanf("%D\N",&I)
How to Implement Interfaces in C++
Directx/C++ 3D Engine Programming: Learn Now, or Wait for Directx 12
How to Use Gpu::Stream in Opencv
Error: Expression Must Have a Class Type
C++ If Else If Not Working Properly
Undefined Reference to Template Members
Why Is the Data Type Needed in Pointer Declarations
Why Can't I Assign an Array Variable Directly to Another Array Variable with the '=' Operator
C++ Get Handle of Open Sockets of a Program
Right Justifying Output Stream in C++
To Stl or !Stl, That Is the Question
Retrieving a C++ Class Name Programmatically
Benefits and Portability of Boost Library
Why Is There No Piecewise Tuple Construction