C की stdlib.h Functions
In this page:
#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()
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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()
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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()
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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()
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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()
// 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;
}
Login to try C/C++/Java/PHP code in the editor
atoiइस्तेमाल करना और एक error को real 0 से न बता पाना;strtolerrors report करता है।- पहले
srand()के बिनाrand()call करना, ताकि हर run same sequence दे। - इन functions इस्तेमाल करने से पहले
#include <stdlib.h>भूल जाना।
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