YOUR C LEARNING COMPANION

Kahan se padhein?

Learning path
C ki buniyadChapter 7 of 73

Variables, values aur types

Data ko meaning dein: declaration, initialization, assignment aur conversion se pehle object, uske naam, type aur current value ka difference samjhein.

13 min readFoundationsC17 baseline
Offline reading
Is chapter mein14 sections
0% through this chapter

int apples = 4; dekhein. Ismein ek naam, ek type aur ek starting value hai. Hum in ideas ko ek-ek karke samjhenge, phir dekhenge ki program nayi value store kare toh kya badalta hai.

1. Data ko interpretation chahiye

Same stored pattern different rules ke under alag meaning le sakta hai: number, character ya colour component. Representation batati hai ki information encode kaise hoti hai. Program ko use interpret karne aur valid operations decide karne ke rules chahiye.

Value program ki use ki hui information hai, jaise integer four. Source spelling 4 se us value ko likhte hain. Stored representation batati hai ki implementation use encode kaise karti hai. Value, uski spelling aur uski storage ko alag sochein.

Type values ke kuch rules deta hai. Isse pata chalta hai ki kaun-si values fit hongi aur kaun-se operations aur conversions apply honge. Jaise do integers ka division floating-point division se alag rules use karta hai. Neeche yeh difference trace karenge.

Iska matlab yeh nahi ki har possible bit pattern har type ki valid value hai. C har type ka universal size ya representation bhi nahi deti. Detail mein jaane par yeh boundaries important hoti hain.

2. Object, name aur value alag hain

C mein data storage ke aise region ko object kehte hain jiske contents values represent kar sakte hain. Neeche apples integer object ka naam hai, jiski starting value four hai. Source code ke naam ko identifier kehte hain. Everyday C discussion mein named object ko aksar variable kehte hain.

C17 · illustrative fragment
int apples = 4;
Name
apples
Declared type
int
Object
Is definition se introduce hua integer object
Initial value
4
Yeh conceptual relationship hai, RAM mein required byte positions ka diagram nahi.

Naam value nahi hai. Object mein baad mein six ho, toh bhi uska naam apples reh sakta hai. Type bhi current value nahi: four aur six dono int mein representable hain.

Storage ki analogy useful hai: value ke liye ek jagah imagine karein, jiska label ho aur rules hon ki wahan kya hold ho sakta hai. Is analogy ki limit yeh hai ki compiler ko har variable ke liye alag visible physical memory box banana required nahi. Woh registers use kar sakta hai, work combine kar sakta hai ya unnecessary storage hata sakta hai, jab tak C ka required behavior preserve ho.

Visual model ke saath samjhein

Value copy hote hue dekhein

VISUAL MODEL

Starting value badlein aur teen lines follow karein. Dono objects ko alag dekhein.

Maan lijiye declarations function ke andar hain.

  1. int apples = 4;
  2. int saved = apples;
  3. apples = 6;
Object · intapplesInitializer tak nahi pahunche
Object · intsavedInitializer tak nahi pahunche

Abhi dono initializers tak nahi pahunche. Trace mein apples ya saved ki starting value abhi nahi dikhayi gayi.

Diagram logical objects aur values dikhata hai. Physical storage aur optimisation implementation par depend karte hain.

Poora trace padhein
  1. Abhi dono initializers tak nahi pahunche. Trace mein apples ya saved ki starting value abhi nahi dikhayi gayi.
  2. Initializer apples ko value 4 deta hai. Later assignment se value badalne par bhi naam aur type same rehte hain.
  3. Initializer saved ko 4 ki copy deta hai. Dono integer objects ki values separate hain.
  4. apples mein ab 6 hai. saved mein pehle copy hui value 4 ab bhi hai.
Aur gehra samjhein: kya object bina naam ke ho sakta hai?

Haan. Allocation se mile objects aur kuch expressions se bane objects ka variable name hona zaroori nahi. Aage pointers addresses ke through objects access karna samjhayenge. Function ka naam bhi identifier hai, lekin C mein function object nahi hai.

3. Declarations information introduce karti hain

Declaration naam aur uski information introduce karti hai. Definition entity ko define bhi karti hai. Ordinary object ki definition storage reserve karwati hai; function ki definition body deti hai. Abhi local integer objects se shuru karein. Multi-file distinction aage useful hoga.

C17 · illustrative fragment
int apples;

Function ke andar ordinary local object ke liye yeh declaration aur definition, dono hai. Yeh apples naam ko int type ke saath introduce karta hai. Yeh object ko explicitly initial value nahi deta. Naam introduce karna aur initial value choose karna alag decisions hain.

Semicolon declaration complete karta hai. Whitespace reader ko parts dekhne mein help karta hai, lekin compiler C ke grammatical rules se parts recognize karta hai. Isko number display karne ya kisi se input maangne ki instruction na samjhein; yahan dono mein se kuch nahi hota.

Aur gehra samjhein: har declaration definition nahi

extern int total; commonly aise object ko declare karta hai jo kahin aur defined hai. Shared header mein declaration ho sakti hai aur ek source file definition de sakti hai. Detailed linkage rules baad mein aayenge; abhi main idea hai ki “naam yahan known hai” aur “object yahan defined hai” alag ho sakte hain.

4. Initialization object ki starting value deti hai

C17 · illustrative fragment
int apples = 4;

Yahan 4 initializer hai: yeh object ki starting value deta hai. Is ordinary local variable ke liye execution declaration tak pahunchne par yeh hota hai. Is form mein = initializer introduce karta hai. Baad mein same symbol assignment expression mein bhi aa sakta hai.

Is ordinary automatic local int ko initializer na dein toh uski indeterminate value hoti hai. Yeh technical term hai; koi usable mystery number nahi. Read karne se pehle valid value dein. Ek run mein zero dikhne se starting-value rule establish nahi hota.

C17 · illustrative fragment
int attempts;       /* yahan usable initial value nahi di gayi */
attempts = 0;        /* ab value assign kar di gayi */

Yeh warning in automatic local objects ke liye hai. Static storage duration wale objects ki initialization rules, explicit initializer na hone par zero values provide karti hain. C mein several storage durations hain; “uninitialized variable hamesha zero hai” aur “hamesha random garbage hai”, dono weak universal rules hain.

Achhi habit hai starting value deliberately choose karna. Counter ne abhi kuch count nahi kiya toh zero suitable hai. Lekin measurement ka real state “abhi available nahi” ho toh zero misleading ho sakta hai. Initialization value deti hai; us value ka meaning program phir bhi decide karta hai.

5. Assignment stored value badalti hai

C17 · illustrative fragment
int apples = 4;
int saved = apples;
apples = 6;

Yahan do objects hain. saved initialize karte waqt apples ki current value read hoti hai aur doosre object ko initialize karti hai. Last line ke baad apples mein six aur saved mein ab bhi four hai. Doosra naam future values ke beech permanent connection nahi banata.

Assignment right side ki value compute karke left side ki suitable destination mein store karti hai. Ordinary non-const integer object familiar destination hai. Formal term modifiable lvalue hai: aisa expression jo assignment se change kiye ja sakne wale object ko designate kare. Full rules aage padhenge.

= symbol mathematical equality assert nahi karta. Is expression mein woh assignment perform karta hai. Do values equal compare hoti hain ya nahi poochhne ke liye C == use karti hai. apples = apples + 1; purani value read karta hai, nayi value compute karta hai aur store karta hai. Signed integers mein computation type ki representable range ke andar rehni chahiye.

6. Type sirf size nahi, ek contract hai

int, char, float aur double alag types hain, sirf alag container sizes nahi. Types ranges, precision, permitted operations aur conversion rules decide karte hain. Type data ke meaning aur requirements ke hisaab se choose karein, sirf isliye nahi ki ek example fit ho raha hai.

ExampleYeh kya express karta hai
int count = 4;Integer quantity, jisme fractional part nahi
double length = 2.5;Floating-point quantity
char grade = 'A';Execution character set se represented character value

C byte woh unit hai jise sizeof measure karta hai; sizeof(char) hamesha one hai. <limits.h> ka CHAR_BIT ek byte ke bits batata hai; yeh kam se kam eight hai. Standard minimum integer ranges guarantee karta hai, har int ko four bytes nahi banata. Exact ranges important hon toh implementation ke limits use karein.

Floating-point types bhi values ka finite set represent karte hain. Approximate real-number calculations ke liye useful hain, lekin har decimal fraction exactly represent nahi kar sakte. Zyada bytes ka matlab automatically “har purpose ke liye correct” nahi.

7. Values convert ho sakti hain

Jab value aisi jagah use ho jahan doosra type required hai, C usko convert kar sakti hai. Conversion us use ke liye value ka type badalti hai; original object ka declared type nahi.

C17 · illustrative fragment
int count = 3;
double measured = count;  /* measured ki starting value 3.0 */
int whole = 3.9;          /* whole ki starting value 3 */

Last line mein int conversion fractional part ko zero ki direction mein discard karti hai, agar resulting integer representable ho. Yeh nearest integer par rounding nahi hai. Out-of-range floating-to-integer conversions mein care chahiye; cast invalid conversion ko safe nahi banata.

C17 · illustrative fragment
double first = 7 / 2;          /* pehle integer division: 3.0 store */
double second = (double)7 / 2; /* floating division: 3.5 store */

7 / 2 ke dono operands integers hain. C pehle integer result three banati hai, phir storage ke liye double mein convert karti hai. Second line mein cast division se pehle operand convert karta hai, isliye floating-point result 3.5 hai. Pehle operation, phir destination conversion padhein.

Aur gehra samjhein: conversions surprise kar sakti hain

Integer promotions aur usual arithmetic conversions kai mixed-type operations decide karti hain. Signed aur unsigned operands especially check karein: C17 mein -1 < 1u false hai, kyunki comparison ke liye signed value unsigned int mein convert hoti hai. Isko apni detailed lesson chahiye; “bada dikhne wala type hamesha jeetta hai” suitable rule nahi.

8. Read-only access aur compile-time constant alag hain

C17 · illustrative fragment
const int maximum_attempts = 3;

const type ko qualify karta hai. Is object ko refer karta expression modifiable destination nahi ban sakta. Originally const define kiye object ko change karna, qualification cast-away karke bhi, undefined behavior hai.

Ek aur distinction hai: mutable object ko const-qualified access path se read karne par usko modify karne ke saare doosre valid ways freeze nahi hote. Pointers mein hum woh case padhenge. Lekin is declaration mein object khud const define hua hai.

C17 mein ordinary const int object automatically integer constant expression nahi hai; kuch language constructs ko yeh specific category chahiye. Everyday word “constant” several different rules ko chhupa sakta hai. C17 mein enumeration constant integer constant expression ko naam dene ka ek tareeka hai.

9. Naam ki visibility aur object ki lifetime alag hain

Scope batata hai ki source code mein identifier kahan use ho sakta hai. Lifetime execution ka woh period hai jab object exist karta hai. Ek program text ki visibility ke baare mein hai; doosra execution ke dauran object ke baare mein.

Is chapter ke ordinary automatic local objects ke liye, execution enclosing block se bahar jaane par lifetime end hoti hai. Us block ki doosri entry naya instance create karti hai. Doosri storage durations ka behavior alag hai: static local object block ki individual executions ke baad bhi exist kar sakta hai.

In rules ke liye particular physical stack layout required nahi. Object-and-value model language model hai jisse hum reason karte hain; implementation machinery ko required behavior respect karna hai, lekin kaam differently organize ho sakta hai.

10. Ideas ko jodiye

Declaration dekhte waqt poochhein: kya introduce ho raha hai, uska type kya hai, value initialize hui ya nahi, naam kahan visible hai aur object kitni der exist karta hai? Assignment dekhte waqt poochhein: kaun-sa object change hota hai aur kaun-si conversions hoti hain?

Ek chhota thought check
C17 · illustrative fragment
int cups = 4;
int remembered = cups;
cups = 6;
double half = 5 / 2;

In statements ke baad remembered aur half ki values kya hain? Output guess karne ke bajaye rules explain karein.

Explanation padhein

remembered four hai: usko pehle wali integer value ki copy mili, future assignments se connection nahi. half 2.0 hai: division ke dono operands integers hain, isliye double mein conversion se pehle integer division two produce karti hai. Destination ka type pehle ho chuke operation ko nahi badalta.

Ab aapke paas better questions poochhne ki vocabulary hai: naam object ko refer karta hai, object ka type aur lifetime hai, aur contents value represent karte hain. Declaration, initialization, assignment aur conversion is story ke different parts hain.

Gehra verify karne ke primary references

Aage badhne se pehle

  • Naam, object, uska type aur current value alag ideas hain.
  • Initialisation starting value deti hai; later assignment doosri suitable value store karti hai.
  • Integer value copy karne se objects ki future values permanently connect nahi hoti.
In ideas ko alag rakhein
Value / Representation
Value information hai; representation us information ki implementation encoding hai.
Apne words mein samjhaayein

apples ki value saved mein copy karne ke baad apples badle toh saved unchanged kyun reh sakta hai?

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.

int saved = apples; ke baad apples change hota hai. saved ka kya hota hai?
Har choice ki explanation padhein
  1. saved automatically apples follow karta hai.

    Initializer ne int value copy ki thi; objects ke beech live connection nahi banaya.

  2. saved apni copied value rakhta hai jab tak use separately badla na jaaye.

    ✓ Sahi reasoning. Objects alag hain. Ek ko badalne se doosre ko assignment nahi hoti.

  3. saved exist karna band kar deta hai.

    apples ko assignment dene se saved ki lifetime end nahi hoti.

C17

Agla idea padhne ke liye tayyar?

Mark this chapter as read whenever you feel ready.

CHAPTER 07 / 73

Is chapter mein

Variables, values aur types

Search all 73 chapters in both languages.EN + HG

Try “pointer”, “why int”, or “undefined behavior”.

Concepts connect. Follow your curiosity.↑ ↓ to explore · Enter to open
MAKE YOURSELF COMFORTABLE

Your reading space

Appearance

A clear canvas with balanced contrast.

Text size

100%
AA

Reading tools

Your data stays in this browser

Export a backup before changing browsers or replacing an older version of the reader.

YOUR PERSONAL MARGINS

My notebook

Saved on this device
A LITTLE CLARITY

The glossary

A starting meaning, then a chapter to go deeper.

A QUICK DEFINITION

A little clarity

Read the full explanation →

A thought worth keeping