← Back to C Course | Chapter 6: Arrays & Strings | Lesson 6 of 8

C के Strings

C में एक string letters की एक row है जो एक special stop marker पर खत्म होती है, एक last car वाली train की तरह। C text ऐसे ही रखता है।
Syntax
c
char string_name[size];

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

printf("%s", string_name);

String क्या है?

यह null-terminator convention है जो strlen() जैसे functions को बिना अलग से length बताए यह जानने देता है कि एक string कहाँ खत्म होती है -- लेकिन इसका यह भी मतलब है कि underlying char array को हमेशा visible text से कम से कम एक byte बड़ा होना चाहिए उस trailing zero को रखने के लिए।

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

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	char name[] = "Hi";
	// Print formatted text to the screen
	printf("%s", name);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Declare और Initialize करना

char name[20] एक fixed 20-byte buffer reserve करता है चाहे आप इसमें actually कितना text store करें, जबकि char name[] = "Hi" compiler को array को exactly initializer plus इसके null terminator (यहाँ 3 bytes) के लिए fit होने देता है।

उदाहरण: Declaring and Initializing

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	char name[20] = "Alex";
	// Print formatted text to the screen
	printf("%s", name);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Null-Terminator

क्योंकि \0 की integer value 0 है, इसे अक्सर conditional checks में falsy कहा जाता है -- while (*str) जैसा एक common idiom तब तक loop करता है जब तक यह terminator तक न पहुंचे, क्योंकि किसी भी दूसरे character की ASCII value nonzero होती है।

उदाहरण: The Null-Terminator

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	char str[] = "Hi";
	// Declare i and set it to 0
	int i = 0;
	// Repeat while str[i] != '/0' is true
	while (str[i] != '\0') {
		i++;
	}
	// Print formatted text to the screen
	printf("%d", i);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Characters Access करना

क्योंकि एक C string सिर्फ एक char array है, name[0] = J सिर्फ पहला character बदलता है; कोई dedicated string-replace function नहीं है, और visible text को छोटा करने वाला कोई भी modification को अभी भी नए अंत में अपना \0 रखना होगा।

उदाहरण: Accessing Characters

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	char name[] = "Jill";
	name[0] = 'J';
	// Print formatted text to the screen
	printf("%s", name);
	// Return 0 to signal that the program finished successfully
	return 0;
}

fgets से Strings को Safely पढ़ना

fgets() एक argument के तौर पर एक maximum buffer size लेता है, इसलिए यह कभी आपके array के अंत से आगे नहीं लिख सकता जैसे gets() कर सकता है -- एक gotcha यह है कि अगर जगह हो तो fgets() trailing newline character रखता है, जो gets() कभी नहीं करता था।

उदाहरण: Reading Strings safely with fgets

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	char buffer[20] = "Sample\n";
	// Print formatted text to the screen
	printf("%s", buffer);
	// 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. null terminator के लिए जगह भूल जाना, जैसे char s[5] = "Hello";, जो fit नहीं होता।
  2. strings को == से compare करना, जो contents नहीं addresses compare करता है; strcmp इस्तेमाल करें।
  3. एक char array को s = "Hi" से assign करना, जो allowed नहीं है; strcpy इस्तेमाल करें।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

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.