top of page

What is string in C

  • Writer: Siddharth Sharma
    Siddharth Sharma
  • Nov 4, 2024
  • 2 min read

Updated: Nov 5, 2024

Strings

Strings  का उपयोग text/characters को store करने के लिए किया जाता है।


For example, "Hello World" is a string of characters.


कई अन्य programming  languages के विपरीत, C में आसानी से string variables बनाने के लिए String type नहीं है।


इसके बजाय, आपको C में एक string बनाने के लिए char प्रकार का उपयोग करना होगा और characters  की एक array बनानी होगी:


Note that you have to use double quotes (" "). 


string को output  करने के लिए, आप C को यह बताने के लिए  format specifier %s के साथ printf() function  का उपयोग कर सकते हैं कि हम अब strings के साथ काम कर रहे हैं:


Access Strings

चूँकि strings actually में C में arrays  हैं, आप square brackets के अंदर इसकी index number का access लेकर string तक पहुंच सकते हैं [].

This example prints the first character (0) in greetings


Note that we have to use the %c format specifier to print a single character.


Modify Strings

किसी string में किसी specific character का value बदलने के लिए, index number देखें और single quotes चिह्नों का उपयोग करें:

Loop Through a String

You can also loop through the characters of a string, using a for loop: 

Another Way Of Creating Strings

उपरोक्त उदाहरणों में, हमने एक string variable बनाने के लिए " string literal " का उपयोग किया। C में string बनाने का यह सबसे आसान तरीका है।


आपको यह भी ध्यान रखना चाहिए कि आप characters के set के साथ एक string बना सकते हैं। यह उदाहरण इस page की शुरुआत में दिए गए उदाहरण के समान result देगा:


Differences

strings बनाने के two method के बीच अंतर यह है कि first  method लिखना आसान है, और आपको \0 character शामिल करने की आवश्यकता नहीं है, क्योंकि C आपके लिए यह करेगा।


आपको ध्यान देना चाहिए कि दोनों arrays का size समान है: उन दोनों में 13 characters हैं (वैसे space भी एक characters के रूप में गिना जाता है), जिसमें \0 character भी शामिल है:



Strings - Special Characters

चूँकि strings को quotes चिह्नों के भीतर लिखा जाना चाहिए, C इस स्ट्रिंग को misunderstand समझेगा, और एक error उत्पन्न करेगा:


The solution to avoid this problem, is to use the backslash escape character.

The backslash (\) escape character turns special characters into string characters:


The sequence \"  inserts a double quote in a string: 



The sequence \'  inserts a single quote in a string: 


The sequence \\  inserts a single backslash in a string: 


Other popular escape characters in C are:



 
 
 

Comments


bottom of page