5.1.21 · D1 · HinglishC Programming

FoundationsSafe alternatives — strncpy, snprintf, strlcpy

1,586 words7 min read↑ Read in English

5.1.21 · D1 · Coding › C Programming › Safe alternatives — strncpy, snprintf, strlcpy

Isse pehle ki tum strncpy, snprintf, ya strlcpy samjho, tumhe yeh dikhna chahiye ki C string memory mein actually kaisi hoti hai. Parent note mein char buf[8], '\0', sizeof, size, aur "off the end" aise use kiya gaya hai jaise yeh sab obvious ho. Yeh page inhe ek-ek karke, kuch nahi se, earn karta hai.


1. Ek byte, ek char, aur boxes ka ek dibba

Puri topic ke liye tumhare dimaag mein yeh picture rehni chahiye: memory ek lambi strip of numbered boxes hai, aur har box ek number hold karta hai jise hum ek letter ki tarah padh sakte hain.

Figure — Safe alternatives — strncpy, snprintf, strlcpy

2. char buf[8] — 8 boxes ki ek fixed row

Figure — Safe alternatives — strncpy, snprintf, strlcpy

Dhyan do ki buf mein kuch bhi built-in nahi jo kahe "meri length 8 hai." Number 8 sirf tumhare source code mein hai, memory mein nahi. Yahi missing length hai jo C string functions ko dangerous banati hai — dekho C strings and the null terminator.


3. '\0' — null terminator (woh "end" marker)

Yeh poori topic ka sabse important symbol hai. C mein ek "string" define hoti hai is tarah:

Figure — Safe alternatives — strncpy, snprintf, strlcpy

4. sizeof vs content length — do alag numbers


5. Pointers, size_t, aur dest/src names


6. Sab kuch jodke: "overflow" kaisa dikhta hai

Figure — Safe alternatives — strncpy, snprintf, strlcpy

Ab parent note ka opening example khud padha jaata hai:

char buf[8];
strcpy(buf, "hello world");   // 12 chars → needs 13 boxes

"hello world" ko boxes chahiye, lekin buf mein sirf 8 hain. strcpy ko sizeof(buf) ki koi jaankari ya check nahi hota, isliye woh box 7 ke baad boxes 8, 9, 10… mein likhta rehta hai — jo doosri cheezoon ki hain — yahi hai Buffer overflow and stack smashing aur Memory safety and undefined behavior ka essence.


Prerequisite map

Byte equals one char box

char buf 8 is a row of boxes

Null terminator marks the end

A C string is boxes plus marker

sizeof is capacity

strlen is content length

strlen must be at most sizeof minus 1

Overflow walks off the row

Safe alternatives strncpy snprintf strlcpy


Equipment checklist

char buf[8] kitne boxes reserve karta hai, aur aakhri legal index kya hai?
8 boxes; aakhri legal index hai buf[7] (indexing 0 se shuru hoti hai).
'\0' byte mein kaunsa number stored hota hai, aur yeh '0' se alag kaise hai?
'\0' mein 0 store hota hai; '0' mein 48 — yeh bilkul alag bytes hain.
k visible characters store karne ke liye k+1 boxes kyun chahiye?
'\0' end-marker ke liye ek extra box chahiye hota hai.
sizeof(buf) kya measure karta hai versus strlen(buf)?
sizeof = total capacity in bytes (compile-time); strlen = '\0' tak visible characters (run time par walk karke).
Content aur capacity ko relate karne wali safety inequality batao.
strlen(buf) <= sizeof(buf) - 1.
Agar C mein koi length store nahi hoti toh string function "jaanta" kaise hai ki string kahan khatam hoti hai?
Woh box 0 se aage chalta rehta hai jab tak '\0' byte nahi milta.
copy(dest, src, size) mein kaunsi side overflow ho sakti hai aur kyun?
dest — yeh itna chhota ho sakta hai ki src jo daal raha hai woh na samaye.
Truncation detect karne ka ek-line rule batao.
Truncation tab hua jab return >= size.

Connections

  • Parent: Safe alternatives
  • C strings and the null terminator
  • sizeof vs strlen
  • strcpy and sprintf (unsafe)
  • Buffer overflow and stack smashing
  • Memory safety and undefined behavior
  • Format strings and printf family