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

C की stdlib.h Functions

stdlib.h library handy general tools रखती है जैसे text को numbers में convert करना, random numbers बनाना, और program खत्म करना। यह एक miscellaneous toolbox है।
Syntax
c
#include <stdlib.h>

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

atoi() और atof() से String to Number Conversion

atoi() और atof() एक string के leading numeric characters को respectively एक int या एक double में convert करते हैं, अगर string बिल्कुल भी एक valid number से शुरू न हो तो चुपचाप 0 return करते हुए — जो इन्हें convenient बनाता है लेकिन एक genuine zero और एक parsing failure में फर्क नहीं कर पाता।

उदाहरण: 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;
}

strtol() और strtod() से Robust Conversion

strtol() और strtod() atoi()/atof() जैसा ही string-to-number conversion करते हैं, लेकिन additionally उस address को लिखते हैं जहाँ parsing रुकी एक आपके दिए end pointer में, आपको exactly detect करने देते हुए कि string का कितना हिस्सा actually consumed हुआ और malformed input पकड़ते हुए जिसे atoi() चुपचाप zero मानकर accept कर लेता।

उदाहरण: 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;
}

rand() और srand() से Random Numbers Generate करना

rand() हमेशा same default state से शुरू होने वाले एक internal algorithm से 0 और RAND_MAX के बीच एक pseudo-random integer produce करता है, इसलिए एक seed value (अक्सर current time) के साथ srand() call करना ही हर program run को genuinely different sequence of numbers produce कराता है।

उदाहरण: 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;
}

exit() और abort() से Program Exit Control

exit() program को जहाँ से भी call हो तुरंत बंद कर देता है, process खत्म होने से पहले कोई भी buffered output flush करते हुए और open files cleanly बंद करते हुए, जबकि abort() उस cleanup के बिना abnormally terminate करता है, आमतौर पर यह signal देने के लिए इस्तेमाल होता है कि एक truly unrecoverable error हुई।

उदाहरण: 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;
}

getenv() से Environment Variables

getenv() operating system के environment में एक named environment variable (जैसे PATH या HOME) ढूंढता है और इसकी value का एक pointer एक string की तरह return करता है, या अगर वह variable set न हो तो NULL — एक program के इसे source में hardcode किए बिना configuration pick up करने का एक common तरीका।

उदाहरण: 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. #}
आम गलतियां
  1. atoi इस्तेमाल करना और एक error को real 0 से न बता पाना; strtol errors report करता है।
  2. पहले srand() के बिना rand() call करना, ताकि हर run same sequence दे।
  3. इन functions इस्तेमाल करने से पहले #include <stdlib.h> भूल जाना।

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.