C++ Data Types

An overview of built-in data types available in C++.

C++ Data Types

Data Type यह निर्धारित करता है कि किसी variable में किस प्रकार का data store किया जाएगा। C++ में अलग-अलग प्रकार के data को store करने के लिए अलग-अलग data types का उपयोग किया जाता है।

उदाहरण के लिए, किसी student की age को store करने के लिए int, percentage को store करने के लिए float या double और grade को store करने के लिए char का उपयोग किया जा सकता है।

याद रखें: Data Type → Variable में store होने वाले data का प्रकार निर्धारित करता है।

Why are Data Types Needed?

Computer memory में अलग-अलग प्रकार के data को store करने के लिए data type की आवश्यकता होती है। Data type compiler को यह समझने में सहायता करता है कि variable में किस प्रकार का data store होगा और उस data पर किस प्रकार के operations किए जा सकते हैं।

उदाहरण:

int age = 17;
float percentage = 85.5f;
char grade = 'A';

यहाँ age में integer, percentage में decimal value और grade में एक character store किया गया है।

Categories of C++ Data Types

C++ के data types को सामान्यतः निम्नलिखित categories में समझा जा सकता है:

  1. Fundamental or Basic Data Types
  2. Derived Data Types
  3. User-Defined Data Types
Category Examples
Fundamental / Basic int, char, float, double, bool, void
Derived Array, Pointer, Reference, Function
User-Defined Class, Structure, Union, Enumeration

1. Fundamental Data Types

Fundamental data types C++ के basic built-in data types हैं। इनका उपयोग सामान्य प्रकार के data को store करने के लिए किया जाता है।

Data Type Used For Example
int Whole numbers int age = 17;
char Single character char grade = 'A';
float Decimal numbers float marks = 85.5f;
double Decimal numbers with greater precision double pi = 3.14159;
bool True or false values bool passed = true;
void No value void display();

Integer Data Type – int

int data type का उपयोग सामान्यतः whole numbers (पूर्णांक) को store करने के लिए किया जाता है। इसमें positive numbers, negative numbers और zero store किए जा सकते हैं।

#include <iostream>
using namespace std;

int main()
{
    int age = 17;
    int temperature = -5;

    cout << "Age = " << age << endl;
    cout << "Temperature = " << temperature;

    return 0;
}
Output:
Age = 17
Temperature = -5
याद रखें: int का उपयोग सामान्यतः बिना decimal वाले numbers के लिए किया जाता है।

Character Data Type – char

char data type का उपयोग एक single character को store करने के लिए किया जाता है। Character को single quotation marks (' ') के अंदर लिखा जाता है।

#include <iostream>
using namespace std;

int main()
{
    char grade = 'A';

    cout << "Grade = " << grade;

    return 0;
}
Output:
Grade = A

Note: 'A' एक character है, जबकि "A" एक string literal है।

Floating-Point Data Type – float

float data type का उपयोग decimal या fractional numbers को store करने के लिए किया जाता है।

#include <iostream>
using namespace std;

int main()
{
    float percentage = 85.5f;

    cout << "Percentage = " << percentage;

    return 0;
}
Output:
Percentage = 85.5

यहाँ f suffix यह दर्शाता है कि literal को float के रूप में treat किया जा रहा है।

Double Data Type – double

double का उपयोग decimal numbers को अधिक precision (शुद्धता) के साथ store करने के लिए किया जाता है। सामान्यतः यह float की तुलना में अधिक precision प्रदान करता है।

#include <iostream>
using namespace std;

int main()
{
    double pi = 3.1415926535;

    cout << pi;

    return 0;
}
Output:
3.14159

Output में दिखाई देने वाले decimal places formatting और stream precision पर निर्भर कर सकते हैं।

Boolean Data Type – bool

bool data type का उपयोग logical values को store करने के लिए किया जाता है। इसके मुख्य values true और false हैं।

#include <iostream>
using namespace std;

int main()
{
    bool passed = true;

    cout << passed;

    return 0;
}
Output:
1

Default output formatting में true को 1 और false को 0 के रूप में display किया जा सकता है।

Void Data Type

void का अर्थ है no value। इसका उपयोग सामान्यतः उन functions के लिए किया जाता है जो कोई value return नहीं करते हैं।

#include <iostream>
using namespace std;

void message()
{
    cout << "Welcome to C++";
}

int main()
{
    message();

    return 0;
}
Output:
Welcome to C++

Size of Data Types

C++ में किसी data type द्वारा उपयोग की जाने वाली memory implementation और system पर निर्भर कर सकती है। किसी data type का actual size जानने के लिए sizeof operator का उपयोग किया जा सकता है।

#include <iostream>
using namespace std;

int main()
{
    cout << "Size of int = " << sizeof(int) << " bytes" << endl;
    cout << "Size of char = " << sizeof(char) << " byte" << endl;
    cout << "Size of float = " << sizeof(float) << " bytes" << endl;
    cout << "Size of double = " << sizeof(double) << " bytes";

    return 0;
}
Sample Output:
Size of int = 4 bytes
Size of char = 1 byte
Size of float = 4 bytes
Size of double = 8 bytes
Important: sizeof का result implementation और platform के अनुसार अलग हो सकता है।

Modifiers of Data Types

C++ में कुछ type modifiers का उपयोग fundamental integer और floating-point types की representation या range को प्रभावित करने के लिए किया जाता है। प्रमुख modifiers हैं:

  • signed
  • unsigned
  • short
  • long

उदाहरण:

short int number1 = 100;
unsigned int number2 = 500;
long int number3 = 100000L;

Signed and Unsigned

signed integer type में positive और negative दोनों values represent की जा सकती हैं। unsigned type में केवल non-negative values represent की जाती हैं।

#include <iostream>
using namespace std;

int main()
{
    int a = -20;
    unsigned int b = 20;

    cout << "Signed = " << a << endl;
    cout << "Unsigned = " << b;

    return 0;
}
Output:
Signed = -20
Unsigned = 20

Short and Long

short और long modifiers का उपयोग integer types की representation और range को प्रभावित करने के लिए किया जाता है। इनका exact size implementation पर निर्भर कर सकता है।

short int smallNumber = 100;
long int largeNumber = 1000000;

Derived Data Types

Derived data types वे data types हैं जो existing data types से बनाए जाते हैं। इनके प्रमुख उदाहरण हैं:

  • Array
  • Pointer
  • Reference
  • Function

Array

Array एक ही data type के multiple values को एक साथ store करने के लिए उपयोग किया जाता है।

#include <iostream>
using namespace std;

int main()
{
    int marks[3] = {80, 85, 90};

    cout << marks[0] << " "
         << marks[1] << " "
         << marks[2];

    return 0;
}
Output:
80 85 90

Pointer

Pointer ऐसा variable है जो किसी दूसरे variable के memory address को store कर सकता है।

int number = 10;
int *ptr = &number;

यहाँ ptr में number का memory address store किया गया है।

Reference

Reference किसी existing variable का दूसरा नाम (alias) होता है।

int number = 10;
int &ref = number;

यहाँ ref, number का reference है।

User-Defined Data Types

Programmer अपनी आवश्यकता के अनुसार कुछ नए data types को define कर सकता है। इन्हें User-Defined Data Types कहा जाता है। इनके प्रमुख उदाहरण हैं:

  • Class
  • Structure
  • Union
  • Enumeration (enum)

Structure

Structure का उपयोग different data types के variables को एक single unit में group करने के लिए किया जा सकता है।

#include <iostream>
using namespace std;

struct Student
{
    int rollNo;
    char grade;
};

int main()
{
    Student s;

    s.rollNo = 10;
    s.grade = 'A';

    cout << "Roll No = " << s.rollNo << endl;
    cout << "Grade = " << s.grade;

    return 0;
}
Output:
Roll No = 10
Grade = A

Enumeration – enum

enum का उपयोग named integral constants का एक set बनाने के लिए किया जाता है।

#include <iostream>
using namespace std;

enum Day
{
    Monday,
    Tuesday,
    Wednesday
};

int main()
{
    Day today = Tuesday;

    cout << today;

    return 0;
}
Output:
1

Default रूप से enumeration के first member को 0 से शुरू किया जाता है, इसलिए यहाँ Tuesday की value 1 है।

Data Type and Variable

Data type और variable का संबंध समझना बहुत महत्वपूर्ण है। Data type यह बताता है कि variable में किस प्रकार का data store होगा, जबकि variable उस data को store करने के लिए नामित memory location प्रदान करता है।

Declaration Meaning
int age; age एक integer variable है।
float percentage; percentage एक floating-point variable है।
char grade; grade एक character variable है।
bool passed; passed एक Boolean variable है।

Choosing the Appropriate Data Type

Program बनाते समय data की प्रकृति के अनुसार appropriate data type का चयन करना चाहिए।

Data Suitable Data Type
Number of students int
Student's grade char
Percentage float / double
Whether student passed bool
Very precise decimal value double

Example Using Different Data Types

#include <iostream>
using namespace std;

int main()
{
    int age = 17;
    float percentage = 86.5f;
    char grade = 'A';
    bool passed = true;

    cout << "Age = " << age << endl;
    cout << "Percentage = " << percentage << endl;
    cout << "Grade = " << grade << endl;
    cout << "Passed = " << passed;

    return 0;
}
Output:
Age = 17
Percentage = 86.5
Grade = A
Passed = 1

Important Points

  • Data type variable में store होने वाले data का प्रकार निर्धारित करता है।
  • int का उपयोग सामान्यतः whole numbers के लिए किया जाता है।
  • char का उपयोग single character के लिए किया जाता है।
  • float और double का उपयोग decimal values के लिए किया जाता है।
  • double सामान्यतः float की तुलना में अधिक precision प्रदान करता है।
  • bool logical values true और false को represent करता है।
  • void का अर्थ no value है और इसका उपयोग functions में commonly किया जाता है।
  • Array, pointer, reference और function derived data types के उदाहरण हैं।
  • Class, structure, union और enum user-defined data types के उदाहरण हैं।
  • sizeof operator किसी type या object द्वारा उपयोग की जाने वाली memory का size जानने के लिए उपयोग किया जाता है।
  • Data type का actual size implementation और platform पर निर्भर कर सकता है।

Board Focus

Exam के लिए याद रखें:
int → Integer / Whole Number
char → Single Character
float → Decimal Number
double → Decimal Number with Greater Precision
bool → True / False
void → No Value
Derived → Array, Pointer, Reference, Function
User-Defined → Class, Structure, Union, enum

Board Important Questions

Very Short Answer Questions

Q1. Data Type क्या है?

Answer: Data Type यह निर्धारित करता है कि variable में किस प्रकार का data store किया जाएगा।

Q2. C++ में integer values के लिए किस data type का उपयोग किया जाता है?

Answer: int data type का उपयोग किया जाता है।

Q3. Character store करने के लिए किस data type का उपयोग किया जाता है?

Answer: char data type का उपयोग किया जाता है।

Q4. Decimal values store करने के लिए किन data types का उपयोग किया जाता है?

Answer: float और double का उपयोग किया जाता है।

Q5. Boolean data type की मुख्य values क्या हैं?

Answer: true और false

Q6. sizeof operator का उपयोग किसलिए किया जाता है?

Answer: किसी data type या object द्वारा उपयोग की जाने वाली memory का size जानने के लिए।

Q7. दो derived data types के नाम लिखिए।

Answer: Array और Pointer।

Short Answer Questions

Q8. C++ के fundamental data types को समझाइए।

Answer: Fundamental data types C++ के basic built-in data types हैं। इनमें int, char, float, double, bool और void प्रमुख हैं। int whole numbers, char single character, float और double decimal values तथा bool logical values को represent करने के लिए उपयोग किए जाते हैं।

Q9. float और double में क्या अंतर है?

float double
Decimal values store करता है। Decimal values को अधिक precision के साथ store कर सकता है।
सामान्यतः कम precision देता है। सामान्यतः अधिक precision देता है।
Example: float x = 5.5f; Example: double x = 5.5;

Q10. Derived data types क्या हैं? उदाहरण दीजिए।

Answer: Existing data types से बनाए गए data types को derived data types कहा जाता है। Array, Pointer, Reference और Function इसके प्रमुख उदाहरण हैं।

Q11. User-defined data types क्या हैं?

Answer: Programmer द्वारा अपनी आवश्यकता के अनुसार define किए गए data types को user-defined data types कहा जाता है। Class, Structure, Union और Enumeration इसके उदाहरण हैं।

Q12. char और string में अंतर बताइए।

char String
एक single character store करता है। Characters के sequence को represent करती है।
Example: char ch = 'A'; Example: string name = "Amit";

Long Answer Questions

Q13. C++ में data types को विभिन्न categories में समझाइए।

Answer: C++ के data types को मुख्य रूप से Fundamental, Derived और User-Defined categories में समझा जा सकता है। Fundamental data types में int, char, float, double, bool और void शामिल हैं। Derived data types में Array, Pointer, Reference और Function आते हैं। User-defined data types में Class, Structure, Union और Enumeration शामिल हैं।

Q14. C++ के विभिन्न fundamental data types को उदाहरण सहित समझाइए।

#include <iostream>
using namespace std;

int main()
{
    int age = 17;
    float percentage = 85.5f;
    double pi = 3.14159;
    char grade = 'A';
    bool passed = true;

    cout << age << endl;
    cout << percentage << endl;
    cout << pi << endl;
    cout << grade << endl;
    cout << passed;

    return 0;
}
Output:
17
85.5
3.14159
A
1

Quick Revision

  • Data Type: Data का प्रकार निर्धारित करता है।
  • int: Whole numbers
  • char: Single character
  • float: Decimal number
  • double: More precise decimal number
  • bool: True / False
  • void: No value
  • Derived: Array, Pointer, Reference, Function
  • User-Defined: Class, Structure, Union, enum
  • sizeof: Size in memory पता करने के लिए
One-Line Revision: C++ में Data Type यह निर्धारित करता है कि variable में किस प्रकार का data store होगा और उस data को किस प्रकार process किया जा सकता है।

Practice Questions

  1. Data Type क्या है और इसकी आवश्यकता क्यों होती है?
  2. C++ के fundamental data types के नाम लिखिए।
  3. int, float, double, char और bool का उपयोग समझाइए।
  4. float और double में क्या अंतर है?
  5. Derived data types क्या हैं? उदाहरण दीजिए।
  6. User-defined data types क्या हैं?
  7. sizeof operator का उपयोग किसलिए किया जाता है?
  8. Signed और unsigned integer में अंतर बताइए।
  9. निम्न code का output बताइए:
    int a = 10;
    float b = 5.5f;
    cout << a + b;
  10. एक C++ program लिखिए जिसमें int, float, char और bool variables declare करके उनकी values display की जाएँ।
  11. C++ data types को Fundamental, Derived और User-Defined categories में उदाहरण सहित समझाइए।
Lesson 20 of 39
On This Page