← Back to C Course | Chapter 1: Introduction & Basics | Lesson 10 of 21

C का Character Data Type

char type एक byte में एक single character store करता है, internally एक छोटे integer (इसके ASCII code) की तरह represented, और text के लिए और, ज़रूरत पड़ने पर, छोटी numeric values के लिए इस्तेमाल होता है।
Syntax
c
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

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare letter and set it to 'B'
	char letter = 'B';
	// Print formatted text to the screen
	printf("%c, size: %zu byte", letter, sizeof(letter));
	// 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

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare digit and set it to '7'
	char digit = '7';
	// Declare tab and set it to '/t'
	char tab = '\t';
	// Print formatted text to the screen
	printf("%c%c%c", digit, tab, digit);
	// 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

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare letter and set it to 'A'
	char letter = 'A';
	// Declare next and set it to letter + 1
	char next = letter + 1;
	// Print formatted text to the screen
	printf("%c", next);
	// Return 0 to signal that the program finished successfully
	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

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// 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 formatted text to the screen
	printf("%d %d", s, u);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Characters Print और Read करना

%c format specifier एक single character दिखाने के लिए printf के साथ और input से एक पढ़ने के लिए scanf के साथ इस्तेमाल होता है, writing और reading character data दोनों में consistently काम करते हुए।

उदाहरण: Printing and Reading Characters

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	char c;
	sscanf("Q", "%c", &c);
	// Print formatted text to the screen
	printf("%c", c);
	// 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. double quotes के साथ char c = "A"; लिखना, जब एक character को A जैसे single quotes चाहिए।
  2. %d के साथ एक char print करना और एक number देखकर surprise होना, जब %d इसका integer code दिखाता है और %c character दिखाता है।
  3. यह मान लेना कि plain char हमेशा signed होता है, जब यह implementation-defined है।

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.