← Back to C Course | Chapter 12: Advanced Topics | Lesson 10 of 20

C stdlib.h Functions

The stdlib.h library holds handy general tools like converting text to numbers, making random numbers, and ending the program. It is a miscellaneous toolbox.
Syntax
c
#include <stdlib.h>

int number = atoi(string);
double number = atof(string);
long number = strtol(string, &end_pointer, base);

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()

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Program execution starts in main()
int main() {
	// Declare n and set it to atoi("42")
	int n = atoi("42");
	// Print formatted text to the screen
	printf("%d", n);
	// Return 0 to signal that the program finished successfully
	return 0;
}

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()

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Program execution starts in main()
int main() {
	char *end;
	// Declare n and set it to strtol("42abc", &end, 10)
	long n = strtol("42abc", &end, 10);
	// Print formatted text to the screen
	printf("%ld", n);
	// Return 0 to signal that the program finished successfully
	return 0;
}

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()

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Program execution starts in main()
int main() {
	srand(1);
	// Declare n and set it to rand() % 100
	int n = rand() % 100;
	// Print formatted text to the screen
	printf("%d", n >= 0 && n < 100);
	// Return 0 to signal that the program finished successfully
	return 0;
}

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()

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("Before exit");
	exit(0);
	// Print formatted text to the screen
	printf("This never runs");
	// Return 0 to signal that the program finished successfully
	return 0;
}

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()

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Program execution starts in main()
int main() {
	char *path = getenv("NONEXISTENT_VAR_XYZ");
	// Print formatted text to the screen
	printf("%d", path == NULL);
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using atoi and being unable to tell an error from a real 0; strtol reports errors.
  2. Calling rand() without srand() first, so every run gives the same sequence.
  3. Forgetting #include <stdlib.h> before using these functions.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.