Standard library: facilities aur contracts
Library families choose, buffer/error contracts read aur hosted, freestanding, C17, C23 aur platform facilities distinguish karein.
Is chapter mein9 sections
Standard library output, allocation aur mathematical calculations jaise common operations deti hai. Function sahi use karne ke liye arguments ka meaning, return result aur caller ki responsibility samjhein. Sirf familiar naam kaafi nahi.
1. Kaam ke according family choose karein
| Header | Main role |
|---|---|
<stdio.h> | Streams, formatted I/O aur file access |
<stdlib.h> | Allocation, numeric conversion, sorting, searching aur program termination |
<string.h> | Byte-region aur null-terminated string operations |
<ctype.h> | Character classification aur case conversion |
<math.h>, <fenv.h> | Mathematical functions aur floating-point environment facilities |
<stddef.h>, <stdint.h>, <limits.h> | Common types, eligible integer types aur range information |
<assert.h>, <errno.h> | Assertion diagnostics aur selected function error reporting |
<time.h>, <locale.h> | Time facilities aur locale-dependent behavior |
Headers types, macros aur declarations expose karte hain; function implementations library se linked, compiler builtins se implemented ya kisi aur tareeke se supplied ho sakti hain. Facility use karne se pehle specified header include karein. Prototype guess na karein aur ek header ke automatically doosra include karne par rely na karein. Table families ka map hai, exhaustive header list nahi.
2. Call se pehle contract padhein
Poochhein: argument types kya hain; kaunse objects live hone chahiye; kitni storage readable/writable hai; ranges overlap kar sakti hain; returned pointer ka owner kaun hai; aur failure kaise report hota hai? Yeh correct use ka part hain, optional implementation details nahi. Example memcpy known byte count nonoverlapping valid regions mein copy karta hai; destination capacity discover nahi karta.
char source[] = "C";
char destination[sizeof source];
memcpy(destination, source, sizeof source);
Fragment ko <string.h> chahiye. Array size mein terminating null character included hai, aur destination us exact size ka separate array hai. Destination ko unknown-capacity buffer pointer se replace karein toh new capacity contract chahiye. Valid source/destination regions overlap kar sakti hain toh memmove use karein; sufficient storage ki requirement phir bhi rehti hai.
3. Small interfaces ke precise argument rules ho sakte hain
#include <assert.h>
#include <ctype.h>
#include <stdio.h>
int main(void)
{
char letter = 'A';
int classification = isalpha((unsigned char)letter);
assert(classification != 0);
int input = EOF;
if (input != EOF) {
(void)isalpha(input);
}
return 0;
}
<ctype.h> functions ko EOF ya unsigned char mein representable value chahiye. Possibly negative plain char ko unsigned char ke through convert karna argument suitable banata hai. getchar se read value ko EOF check tak int mein rakhein; pehle cast karne se distinction lose ho sakti hai. Classification zero ya nonzero return karti hai; true ke liye exactly one required nahi.
4. Har interface ka failure convention samjhein
malloc null pointer return kar sakta hai; file operation EOF, short count ya error indicator report kar sakta hai; strtol ko end pointer aur range checks chahiye. Kuch interfaces errno use karti hain, par C ise universal last-error channel nahi banati. Operation ke documented rules ke according inspect karein aur relevant error information doosri call se change hone se pehle save karein.
Assertions internal expectations express karti hain aur <assert.h> include karne se pehle NDEBUG define karke disable ho sakti hain. Required input validation ya required side effects sirf assert mein na rakhein. Examples assertions-enabled learning build mein checks ke liye assertions use karti hain. Later input unit actual validation aur stream-error handling develop karegi.
5. Hosted aur freestanding implementations different hain
Hosted implementation hosted execution environment aur standard library supply karti hai, standard feature conditions ke subject. Freestanding implementation ki startup aur library requirements different hain; kernels ya embedded systems jaise environments mein useful. Yeh simply smaller executable nahi, aur embedded automatically freestanding imply nahi karta.
C17 ka minimum freestanding header set <float.h>, <iso646.h>, <limits.h>, <stdalign.h>, <stdarg.h>, <stdbool.h>, <stddef.h>, <stdint.h> aur <stdnoreturn.h> hai. Hosted I/O ya allocation guaranteed nahi. C17 specified optional features, including threads aur atomics, absent allow karta hai; int32_t jaise exact-width integer typedefs requirements meet hone par hi exist karte hain.
6. Standard C aur platform additions distinguish karein
Sockets, directory traversal, C threads interface ke bahar operating-system threads aur graphical windows sab C standard library se guaranteed nahi. POSIX, Windows aur third-party libraries additional interfaces define karti hain. Useful ho sakti hain, par availability aur build instructions explicit platform contract ka part honi chahiye. Standard math functions ko bhi particular toolchain par separate link option chahiye ho sakti hai.
Primary references aur aage ki reading
- WG14 N1570 — public C11 committee draft: 4p6–p7, 5.1.2, 7.1, 7.2, 7.4, 7.5, 7.20.1.1, 7.24.2. C17 baseline ke stable core rules ke liye; final ISO publication nahi.
- WG14 N3096 — public C23 working draft: 4, 7.18, 7.20. C23 notes C17 example checks se separate hain.
Focused explanations ke liye earlier prerequisites ke baad searching aur sorting, dates aur clocks aur floating-point math padhein.
Aage badhne se pehle
- Library facilities supported jobs ke hisaab se organised hain.
- Har call ke argument, storage, result aur failure rules hain.
- Hosted, freestanding, optional aur platform facilities ki availability contracts alag hain.
In ideas ko alag rakhein
- Function signature / Complete contract
- Types capacities, overlap restrictions, ownership ya har valid argument range automatically express nahi karte.
Pointer aur count dene se pehle library function ki documentation mein kya check karenge?
Optional: bolkar samjhein ya note rakhein. Tayyar hon toh aage badh sakte hain.Apni understanding check karein
Optional hai. Answers save ya send nahi hote; chapter kabhi lock nahi hota.
Har choice ki explanation padhein
- Haan, library saare invalid pointers repair karti hai.
Library interface caller ki pointer aur lifetime obligations nahi hataata.
- Haan, header successful resource acquisition guarantee karta hai.
Allocation aur file opening jaise operations phir bhi fail ho sakte hain.
- Nahi. Caller ko function ke argument aur state contracts bhi satisfy karne hote hain.
✓ Sahi reasoning. Bounds, formats, lifetimes aur error handling correct use ka part rehte hain.
C17
Earlier edition ka quick reference
Existing section links aur saved passages ke liye retained. Expanded explanation upar hai; documented corrections source review mein recorded hain.
C standard library Python ya Java jaisi languages ke comparison mein intentionally chhoti hai — yeh C ki philosophy ko reflect karta hai, jahan sirf essential cheezein provide ki jaati hain aur baaki sab programmer ya third-party libraries par chhod diya jaata hai. Iske functions kuch functional families mein aate hain:
- Input/Output (
stdio.h):printf/scanffamilies, character I/O (getchar,putchar), line I/O (fgets,fputs), aur file operations (fopen,fclose,fread,fwrite). - String aur Memory (
string.h): copying (strcpy,memcpy), concatenation (strcat), comparison (strcmp,memcmp), searching (strchr,strstr), aur tokenizing (strtok). - General Utilities (
stdlib.h): dynamic memory (malloc,calloc,realloc,free), numeric conversions (atoi,atof,strtol), sorting aur searching (qsort,bsearch), process control (exit,abort,system), aur pseudo-random numbers (rand,srand). - Mathematics (
math.h): trigonometric, exponential, logarithmic, aur rounding functions (sin,pow,sqrt,log,floor,ceil). - Character handling (
ctype.h):isalpha,isdigit,toupperaurtolowerjaise classification/case conversion functions. ArgumentEOFyaunsigned charmein representable value hona chahiye. Possibly negative plainchar cke liyeisalpha((unsigned char)c)use karein;getcharresult meinEOFcheck karne se pehle usko cast karke na kho dein. - Date aur Time (
time.h):time,clock,difftime,strftime. - Diagnostics (
assert.h,errno.h): runtime assertions aur structured error reporting.
Baad ke sections in families ko detail mein revisit karte hain: dynamic memory (Section 22), formatted I/O (Section 23), aur file handling (Section 24).
Agla idea padhne ke liye tayyar?
Mark this chapter as read whenever you feel ready.