C++ का char Data Type
In this page:
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Character Literals
एक character literal single quotes के बीच लिखा जाता है, जैसे A या 7, और tab के लिए '\t' या newline के लिए '\n' जैसे escape sequences इस्तेमाल करके special characters भी represent कर सकता है।
उदाहरण: Character Literals
// 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;
}
Login to try C/C++/Java/PHP code in the editor
छोटे Integers के तौर पर Characters
Internally, एक char अपने ASCII code को represent करने वाले एक छोटे integer की तरह store होता है, जिसका मतलब है arithmetic operations सीधे characters पर काम करते हैं, code को numbers add या subtract करके एक letter से दूसरे में shift करने देते हुए।
उदाहरण: Characters as Small Integers
#include <iostream>
int main() {
char letter = 'A';
char next = letter + 1; // shifts to 'B' via ASCII arithmetic
std::cout << next << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
cin और cout के साथ char
cin और cout से एक single char पढ़ना और लिखना सीधे काम करता है, किसी special format specifier के बिना, क्योंकि C++ के stream operators automatically जानते हैं कि हर type कैसे handle करना है।
उदाहरण: char with cin and cout
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- एक character के लिए double quotes इस्तेमाल करना, जैसे
char c = "A";, जो एक string literal है और compile नहीं होता; एक char literal को single quotesAचाहिए। - यह उम्मीद करना कि
cout << c + 1एक character print करेगा, जब एक int add करना result कोintमें promote कर देता है और एक number print करता है। - यह मान लेना कि plain
charहमेशा signed होता है, जब यह signed है या unsigned यह platform पर depend करता है।
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first: