C stdlib.h Functions
In this page:
String to Number Conversion with atoi() and atof()
atoi() and atof() convert the leading numeric characters of a string into an int or a double respectively, silently returning 0 if the string doesn't start with a valid number at all — which makes them convenient but unable to distinguish a genuine zero from a parsing failure.
Example: String to Number Conversion with atoi() and atof()
#include <stdio.h>
#include <stdlib.h>
int main() {
int n = atoi("42");
printf("%d", n);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Robust Conversion with strtol() and strtod()
strtol() and strtod() perform the same kind of string-to-number conversion as atoi()/atof(), but additionally write the address where parsing stopped into an end pointer you supply, letting you detect exactly how much of the string was actually consumed and catch malformed input that atoi() would silently accept as zero.
Example: Robust Conversion with strtol() and strtod()
#include <stdio.h>
#include <stdlib.h>
int main() {
char *end;
long n = strtol("42abc", &end, 10);
printf("%ld", n);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Generating Random Numbers with rand() and srand()
rand() produces a pseudo-random integer between 0 and RAND_MAX using an internal algorithm that always starts from the same default state, so calling srand() with a seed value (often the current time) is what makes each program run produce a genuinely different sequence of numbers.
Example: Generating Random Numbers with rand() and srand()
#include <stdio.h>
#include <stdlib.h>
int main() {
srand(1);
int n = rand() % 100;
printf("%d", n >= 0 && n < 100);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Program Exit Control with exit() and abort()
exit() shuts the program down immediately from wherever it's called, flushing any buffered output and closing open files cleanly before the process ends, whereas abort() terminates abnormally without that cleanup, typically used to signal that a truly unrecoverable error occurred.
Example: Program Exit Control with exit() and abort()
#include <stdio.h>
#include <stdlib.h>
int main() {
printf("Before exit");
exit(0);
printf("This never runs");
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Environment Variables with getenv()
getenv() looks up a named environment variable (like PATH or HOME) in the operating system's environment and returns a pointer to its value as a string, or NULL if that variable isn't set — a common way for a program to pick up configuration without hardcoding it into the source.
Example: Environment Variables with getenv()
#include <stdio.h>
#include <stdlib.h>
int main() {
char *path = getenv("NONEXISTENT_VAR_XYZ");
printf("%d", path == NULL);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 20 topics to unlock
0/20 topics done
Complete these topics first:
- C typedef
- C Type Casting
- C Bit Fields
- C Variable Length Arrays
- C Command Line Arguments
- C Function Pointers
- C Callback Functions
- C Multidimensional Pointer
- C string.h Functions
- C stdlib.h Functions
- C math.h Functions
- C time.h Functions
- C ctype.h Functions
- C errno.h
- C assert.h
- C Error Handling
- C Debugging Techniques
- C Code Style & Best Practices
- C Common Mistakes
- C Interview Questions