C Helper Functions
C has a large amount of helper libraries and functions.
stdlib.h
-
char *getenv(const char *name)
- Get environment variablename
- Name of the environment variable[4]
-
long int strtol(const char *nptr, char **endptr, int base)
- Converts a string into a long integer[1-2]nptr
- String to be readendPtr
- On success, points to first char after number; on failure,NULL
base
- Base for number conversion- Returns converted number as
long
-
rand
andsrand
- Random number generation[5]void srand(unsigned int seed)
- Set a seed for the pseudo-random number generatorint rand(void)
- Return a pseudo-random number- e.g.
srand(clock()); int num = rand();
stdio.h
-
FILE *fopen(const char *path, const char *mode)
- Open the file and return a stream of it.path
- To the file to be opened-
mode
- How the file should be openedr
/r+
- Open the file for reading / reading and writing, positioning the stream at the beginning of the filew
/w+
- Open file for writing / reading and writing, truncating file if it exists, creating file if it doesn't, positioning stream at beginning of the filea
/a+
- Open file for appending / reading and appending, creating file if it doesn't exist, positioning stream at the end of the file
References
- https://linux.die.net/man/3/strtol
- https://codeforwin.org/2018/01/convert-string-to-long-using-strtol-c.html
- https://linux.die.net/man/3/fopen
- https://www.tutorialspoint.com/c_standard_library/c_function_getenv.htm
- https://linux.die.net/man/3/srand
Last modified: 202401040446