← Back to C++ Course | Chapter 6: Arrays & Strings | Lesson 7 of 15

C++ की Strings (C-style)

एक C-style string letters की एक row है जो एक special stop marker पर खत्म होती है, एक last car वाली train की तरह। यह text रखने का पुराना तरीका है।
Syntax
cpp
char string_name[size];

char string_name[] = "text";  // ends with a hidden '\0'

C-style String क्या है?

एक C-style string बस एक special convention वाला एक character array है: यह हमेशा एक null-terminator character, '\0', से खत्म होता है, जो mark करता है कि actual text कहाँ रुकता है भले ही array खुद में extra unused space हो।

हर standard C string function यह जानने के लिए उस terminator को ढूंढने पर depend करता है कि कहाँ रुकना है।

उदाहरण: What is a C-style String?

cpp
#include <iostream>

int main() {
	char greeting[] = "Hi";
	std::cout << greeting << std::endl; // stops printing at the null-terminator
	return 0;
}

Declare और Initialize करना

आप एक C-style string array को explicitly size कर सकते हैं, जैसे char name[20];, बाद में बढ़ने के लिए जगह छोड़ते हुए, या compiler को इसे एक string literal से automatically size करने दें, जैसे char name[] = "Alex";, जो array को exactly text plus terminator में fit करने के लिए size करता है।

उदाहरण: Declaring and Initializing

cpp
#include <iostream>
#include <cstring>

int main() {
	char name[20];
	strcpy(name, "Alex");
	char city[] = "Delhi"; // compiler sizes this automatically
	std::cout << name << " " << city << std::endl;
	return 0;
}

Null-Terminator (\0)

Null-terminator ही वह है जो एक C-style string को characters के एक ordinary array से अलग करता है — इसके बिना, strlen() या cout << जैसे functions को यह जानने का कोई तरीका नहीं होगा कि meaningful text कहाँ खत्म होता है और बचा हुआ, uninitialized memory कहाँ शुरू होती है, और intended end से आगे पढ़ते (और शायद crash या garbage print करते) रहेंगे।

उदाहरण: The Null-Terminator (\0)

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

// Program execution starts in main()
int main() {
	char word[] = {'H', 'i', '\0'};
	// Print to the console with cout
	std::cout << word << " length=" << strlen(word) << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Characters Access और Modify करना

एक C-style string के अंदर individual characters को ordinary array indexing इस्तेमाल करके पढ़ा या overwrite किया जा सकता है, जैसे name[0] = J;, exactly जैसे आप किसी दूसरे char array के साथ करते — बस terminating '\0' को कभी overwrite या accidentally remove न करने के लिए careful रहें।

उदाहरण: Accessing and Modifying Characters

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

// Program execution starts in main()
int main() {
	char name[] = "jack";
	name[0] = 'J';
	// Print to the console with cout
	std::cout << name << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

C-style Strings को Safely पढ़ना

cin >> से सीधे एक C-style string में input पढ़ना पहले whitespace character पर रुकता है, जो spaces रखने वाली किसी भी चीज़ के लिए fail होता है — cin.getline(buffer, size) इसकी बजाय एक specified maximum length तक एक पूरी line पढ़ता है, अगर input उम्मीद से लंबा हो तो एक buffer overflow safely avoid करते हुए।

उदाहरण: Reading C-style Strings safely

cpp
#include <iostream>

int main() {
	char fullName[50] = "Alex Johnson"; // cin >> would stop at the space
	std::cout << fullName << std::endl;
	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. null terminator के लिए जगह भूल जाना, जैसे char s[5] = "Hello";, जो fit नहीं होता।
  2. C-strings को == से compare करना, जो contents नहीं addresses compare करता है; strcmp इस्तेमाल करें।
  3. एक array को s = "Hi" से assign करना, जो allowed नहीं है; strcpy इस्तेमाल करें।

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.