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

C++ का char Data Type

char type एक byte में एक single character store करता है, internally एक छोटे integer (इसके ASCII code) की तरह represented, text के लिए और, ज़रूरत पड़ने पर, छोटी numeric values के लिए इस्तेमाल होता है।
Syntax
cpp
char variable_name = 'character';
char escape_name = '\\n';

char Type

char type एक single character store करता है, exactly एक byte memory occupy करते हुए, और C++ programs में individual letters, digits, symbols, या छोटी integer values के लिए इस्तेमाल होता है।

उदाहरण: The char Type

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

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

Character Literals

एक character literal single quotes के बीच लिखा जाता है, जैसे A या 7, और tab के लिए '\t' या newline के लिए '\n' जैसे escape sequences इस्तेमाल करके special characters भी represent कर सकता है।

उदाहरण: Character Literals

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

// Program execution starts in main()
int main() {
	// Declare letter and set it to 'A'
	char letter = 'A';
	// Declare tab and set it to '/t'
	char tab = '\t';
	char newline = '\n';
	// Print to the console with cout
	std::cout << letter << tab << "end" << newline;
	// Return 0 to signal that the program finished successfully
	return 0;
}

छोटे Integers के तौर पर Characters

Internally, एक char अपने ASCII code को represent करने वाले एक छोटे integer की तरह store होता है, जिसका मतलब है arithmetic operations सीधे characters पर काम करते हैं, code को numbers add या subtract करके एक letter से दूसरे में shift करने देते हुए।

उदाहरण: Characters as Small Integers

cpp
#include <iostream>

int main() {
	char letter = 'A';
	char next = letter + 1; // shifts to 'B' via ASCII arithmetic
	std::cout << next << std::endl;
	return 0;
}

signed char बनाम unsigned char

char default से signed है या unsigned यह compiler पर depend करता है, इसलिए signed char और unsigned char explicitly negative numbers शामिल range और positive numbers में आगे तक जाने वाली range के बीच choose करते हैं।

उदाहरण: signed char vs unsigned char

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

// Program execution starts in main()
int main() {
	// Declare s and set it to -100
	signed char s = -100;
	// Declare u and set it to 200
	unsigned char u = 200;
	// Print to the console with cout
	std::cout << (int)s << " " << (int)u << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

cin और cout के साथ char

cin और cout से एक single char पढ़ना और लिखना सीधे काम करता है, किसी special format specifier के बिना, क्योंकि C++ के stream operators automatically जानते हैं कि हर type कैसे handle करना है।

उदाहरण: char with cin and cout

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

// Program execution starts in main()
int main() {
	// Declare grade and set it to 'B'
	char grade = 'B';
	// Print to the console with cout
	std::cout << "Grade: " << grade << std::endl;
	// 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. एक character के लिए double quotes इस्तेमाल करना, जैसे char c = "A";, जो एक string literal है और compile नहीं होता; एक char literal को single quotes A चाहिए।
  2. यह उम्मीद करना कि cout << c + 1 एक character print करेगा, जब एक int add करना result को int में promote कर देता है और एक number print करता है।
  3. यह मान लेना कि plain char हमेशा signed होता है, जब यह signed है या unsigned यह platform पर depend करता है।

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.