Data types in C++

एक data type तय करता है एक value को कितने bytes मिलते हैं और इस पर कौन से operations meaningful हैं, और C++ उन C types के ऊपर bool और references जोड़ता है जो आप पहले से जानते हैं।

9 min read · 10 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

वे 50 paise जो ग़ायब हो गए

Canteen till chai की price एक int में store करता है:

int price = 12.50;

C++ चुपचाप 12 रखता है और .50 गिरा देता है। कोई notice नहीं करता, जब तक owner loss multiply नहीं करता: दिन में लगभग 900 chai पर 50 paise यानी Rs 450, रोज़ ग़ायब, सिर्फ़ एक ग़लत type choice की वजह से।

एक data type कोई formality नहीं है। यह तय करता है एक variable क्या hold कर सकता है और चुपचाप क्या फेंक देता है।

Theory

Counter के पीछे containers

Canteen कभी dal को paper bag में या chai को thali में serve नहीं करता। हर container की एक shape, एक capacity, और वह चीज़ें जो यह safely hold कर सकता है होती हैं।

Data types एक program के containers हैं: int whole numbers के लिए steel tumbler है, double decimals के लिए wide bowl, char एक character के लिए छोटी sauce cup, bool एक two-position switch। ग़लत container चुनिए और content spill हो जाता है।

Theory

एक data type असल में क्या तय करता है

एक data type compiler को एक variable के बारे में तीन चीज़ें बताता है:

  • Size: कितने bytes reserve करने हैं (एक int को आमतौर पर 4 मिलते हैं)।
  • Interpretation: bits का मतलब क्या है (वही bits एक number या एक character हो सकते हैं)।
  • Operations: क्या legal है (आप ints divide कर सकते हैं, bools नहीं)।

C++ types को तीन families में group करता है: fundamental (built-in), derived (दूसरों से बने), और user-defined (आप बनाते हैं)।

At a glance

Fundamental types एक नज़र में (typical sizes)

TypeSizeHolds
char1 byteएक character: 'A' (ASCII code के रूप में stored)
int4 bytesWhole numbers: 12, -900
float4 bytesDecimals, लगभग 6-7 digits precision
double8 bytesDecimals, लगभग 15 digits precision
bool1 bytetrue या false
void0कोई value नहीं (function कुछ return नहीं करता)

Theory

C++ उस C के ऊपर क्या जोड़ता है जो आप जानते हैं

BCA104 से आप int, float, char और modifiers short, long, signed, unsigned जानते हैं। C++ नए pieces लाता है:

  • bool: एक असली true/false type (पुराना C इसे 0 और 1 से नकली बनाता था)।
  • Reference (int& r = x;): एक मौजूदा variable के लिए दूसरा नाम, बाद में functions में भारी इस्तेमाल होता है।
  • wchar_t: बड़े character sets के लिए wide characters।

Derived types: array, pointer, function, reference। User-defined types: class, struct, union, enum, और class वह जगह है जहाँ यह subject रहता है।

Think first

Output का अंदाज़ा लगाइए

Till programmer लिखता है:

char c = 65;

cout << c;

पाँच seconds लीजिए: screen पर क्या दिखता है, 65 या कुछ और?

Show the answer

यह A print करता है। एक char एक छोटा number store करता है और इसे एक character के रूप में interpret करता है: 65 'A' का ASCII code है। वही bits, अलग meaning, क्योंकि type interpretation control करता है। अगर आपको 65 देखना है, तो इसे एक int में store कीजिए या cast कीजिए: cout << (int)c;।

Quiz

Canteen को price 12.50 और बाद में 84756.75 जैसे totals बिना paise खोए store करने हैं। कौन सा type exam-safe choice है?

  1. double
  2. int
  3. char
  4. bool
Show the answer

double

double decimals को लगभग 15 digits precision के साथ hold करता है, money-sized values के लिए comfortably exact। int 12.50 को 12 तक truncate करता है (वे ग़ायब हुए paise)। char एक character hold करता है, bool सिर्फ़ true/false। अगर आपने float सोचा: यह काम करता है, पर इसकी 6-7 digit precision बड़े totals पर डगमगाना शुरू करती है, तो double लिखा हुआ safer answer है।

Watch out

दो silent money-losers

Integer division: 5 / 2 2 है, 2.5 नहीं। दोनों operands ints हैं, तो result एक int है। 2.5 पाने के लिए 5.0 / 2 लिखिए।

Hardcoded sizes: किसी exam में "int 4 bytes का है" को कभी universal fact मत लिखिए। Sizes machine और compiler पर depend करते हैं, तो "typically 4 bytes" लिखिए और check करने के तरीक़े के रूप में sizeof(int) mention कीजिए। Examiners specifically यह word ढूँढते हैं।

Formula

वह classification tree जो exams आपसे बनवाते हैं

Data types

├── Fundamental: int, char, float, double, bool, void

├── Derived: array, pointer, function, reference

└── User-defined: class, struct, union, enum

एक 5-mark "C++ में data types explain कीजिए" answer यही tree है plus हर family के लिए एक line और एक example। इसे एक बार memory से draw करने का practice कीजिए।

Summary

Key takeaways

  • एक data type bytes में size, bits की interpretation, और legal operations तय करता है।
  • तीन families: fundamental (int, char, float, double, bool, void), derived (array, pointer, function, reference), user-defined (class, struct, union, enum)।
  • C++ over C: एक असली bool, references (int&), wchar_t।
  • char एक ASCII code store करता है: char c = 65 A print करता है।
  • Money को double चाहिए; int truncate करता है और 5/2 है 2 (integer division)।
  • Sizes machine dependent हैं: "typically" लिखिए, sizeof से check कीजिए।
  • Memory hook: सही food के लिए सही container।

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Concepts of OOPS

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Data types in C++ · Object Oriented Programming and Data Structures (OOPs & D.S.) · Gri-Learn