← Back to C++ Course | Chapter 1: Introduction & Basics | Lesson 9 of 15

C++ के Data Types

Data types C++ को बताते हैं कि एक value किस तरह की चीज़ है, जैसे whole numbers, decimals, letters, या true-false। यह अलग-अलग items के लिए अलग-अलग आकार के boxes रखने जैसा है।
Syntax
cpp
data_type variable_name = value;

// e.g. int, char, float, double, bool

Integer Type

int type बिना किसी fractional component के whole numbers store करता है, और ज़्यादातर modern 32-bit और 64-bit systems पर यह 4 bytes occupy करता है, इसे लगभग -2 billion से +2 billion तक की range देते हुए।

counters, indexes, और किसी भी ऐसी value के लिए int इस्तेमाल करें जिसे कभी decimal point की ज़रूरत नहीं पड़ेगी।

उदाहरण: Integer Type

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare counter and set it to 2000000
	int counter = 2000000;
	// Print to the console with cout
	std::cout << "Counter: " << counter << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Character Type

char type exactly 1 byte में एक single character store करता है, पूरी strings के लिए इस्तेमाल होने वाले double quotes की बजाय A जैसे single quotes इस्तेमाल करते हुए।

Internally, एक char असल में सिर्फ एक छोटा integer है जो ASCII table में उस character की position represent करता है, यही वजह है कि आप characters पर सीधे arithmetic perform कर सकते हैं।

उदाहरण: Character Type

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare grade and set it to 'A'
	char grade = 'A';
	// Print to the console with cout
	std::cout << "Grade: " << grade << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Floating-Point Types

float और double दोनों decimal point वाले numbers store करते हैं, लेकिन वे precision के लिए memory trade off करते हैं: float 4 bytes और लगभग 7 significant digits की accuracy इस्तेमाल करता है, जबकि double लगभग 15-16 digits के लिए 8 bytes इस्तेमाल करता है।

ज़्यादातर C++ code default से double इस्तेमाल करता है जब तक memory tightly constrained न हो, क्योंकि extra precision subtle rounding errors से बचाती है।

उदाहरण: Floating-Point Types

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare price and set it to 9.99f
	float price = 9.99f;
	// Declare pi and set it to 3.14159265358979
	double pi = 3.14159265358979;
	// Print to the console with cout
	std::cout << price << " " << pi << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Boolean Type

bool type सिर्फ दो possible values रखता है, true या false, और यह किसी भी yes/no condition represent करने वाली चीज़ के लिए natural type है, जैसे क्या एक user logged in है।

एक int flag (0 या 1) की बजाय bool इस्तेमाल करना conditional code को self-documenting बनाता है, क्योंकि isLoggedIn loggedInFlag == 1 से कहीं ज़्यादा clearly पढ़ता है।

उदाहरण: Boolean Type

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare isLoggedIn and set it to true
	bool isLoggedIn = true;
	// Print to the console with cout
	std::cout << "Logged in: " << isLoggedIn << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Void Type

void एक value की deliberate absence represent करता है, सबसे ज़्यादा एक function के return type की तरह दिखता है जो एक action perform करता है लेकिन caller को कुछ वापस नहीं देता, जैसे void printMessage()।

यह कुछ lower-level contexts में भी इस्तेमाल होता है, जैसे एक generic pointer type void* जो किसी भी type के data की ओर point कर सकता है।

उदाहरण: Void Type

cpp
// Include std::cout and std::cin
#include <iostream>

void printMessage() {
	// Print to the console with cout
	std::cout << "No value returned" << std::endl;
}

// Program execution starts in main()
int main() {
	printMessage();
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. decimals वाली एक value के लिए int इस्तेमाल करना जैसे int price = 9.99;, जो चुपचाप 9 तक truncate हो जाता है।
  2. यह भूल जाना कि char एक single character रखता है और char name = "Bob"; लिखना, जो compile नहीं होता।
  3. यह मान लेना कि int या long का size हर platform पर same है, जब standard सिर्फ minimum sizes guarantee करता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.