C के Strings
In this page:
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?
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Null-Terminator
क्योंकि \0 की integer value 0 है, इसे अक्सर conditional checks में falsy कहा जाता है -- while (*str) जैसा एक common idiom तब तक loop करता है जब तक यह terminator तक न पहुंचे, क्योंकि किसी भी दूसरे character की ASCII value nonzero होती है।
उदाहरण: The Null-Terminator
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Characters Access करना
क्योंकि एक C string सिर्फ एक char array है, name[0] = J सिर्फ पहला character बदलता है; कोई dedicated string-replace function नहीं है, और visible text को छोटा करने वाला कोई भी modification को अभी भी नए अंत में अपना \0 रखना होगा।
उदाहरण: Accessing Characters
// 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;
}
Login to try C/C++/Java/PHP code in the editor
fgets से Strings को Safely पढ़ना
fgets() एक argument के तौर पर एक maximum buffer size लेता है, इसलिए यह कभी आपके array के अंत से आगे नहीं लिख सकता जैसे gets() कर सकता है -- एक gotcha यह है कि अगर जगह हो तो fgets() trailing newline character रखता है, जो gets() कभी नहीं करता था।
उदाहरण: Reading Strings safely with fgets
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- null terminator के लिए जगह भूल जाना, जैसे
char s[5] = "Hello";, जो fit नहीं होता। - strings को
==से compare करना, जो contents नहीं addresses compare करता है;strcmpइस्तेमाल करें। - एक char array को
s = "Hi"से assign करना, जो allowed नहीं है;strcpyइस्तेमाल करें।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: