String series of characters या group of characters होती है , C में string define करने के लिए double quotes का use किया जाता है।


C language में string को बाकी programming languages like PHP , C++, JavaScript etc से अलग अलग तरह से define किया जाता है। String define करने के लिए C language में variable को square brackets के साथ define किया जाता है। जो एक null value \0 से terminate होती है।
For Example -

char s[] = "my string";

जब भी compiler group of characters को एक साथ double quotes में determine (identify) करता है automatically end में \0 value को append कर देता है।

my string\0
Memory Diagram

C String Example

CopyFullscreenClose FullscreenRun
#include <stdio.h>
int main() {
  char name[] = "Rahul Kumar Verma";
  // use %s specifier to print string.
  printf("Name : %s", name);
  return 0;
}
Output
Name : Rahul Kumar Verma

C define string size

हालाँकि अगर आप चाहे तो string का size भी define कर सकते हैं -

CopyRun
#include <stdio.h>
int main() {
  // pass the length of string brackets.
  char words[5] = "Hello";
  printf("%s", words);
  return 0;
}
Output
Hello

size define करने के बाद अगर आप ज्यादा length की string assign करते हैं तो , define की गयी length के जितने ही string capture होगी .

char words[10] = "Hello ! welcome to learnhindituts";
printf("%s", words);

// Output : Hello ! we

Alternative ways to define string in C

हालाँकि need के according आप कई तरह से string define कर सकते हैं।

char s[] = "abcd";

char s[50] = "abcd";

char s[] = {'a', 'b', 'c', 'd', '\0'};

char s[5] = {'a', 'b', 'c', 'd', '\0'};

C string input from user

CopyRun
#include <stdio.h>
int main() {
  char name[20];
  printf("Enter name: ");
  scanf("%s", name);
  printf("Your name is %s.", name);
  return 0;
}
Output
Enter name: Rahul Kumar
Your name is Rahul.

ऊपर दिए गए Example को अगर आप ध्यान से देखोगे तो आप पाओगे कि , enter तो Rahul Kumar किया था लेकिन सिर्फ Rahul ही हमें output में मिला है। ऐसा इसलिए हुआ क्योंकि हमें Rahul के बाद space दिया था। जबकि character में space नहीं होता। इसलिए हमें character capture करने के बजाय complete line capture करनी होगी।


इसके अलावा &name की जगह name scanf() function में pass किया गया है।

C read line

Now , character की जगह complete line read करने के लिए हम predefined function fgets() का use करते हैं।

CopyRun
#include <stdio.h>
int main() {
  char name[30];
  printf("Enter name: ");
  fgets(name, sizeof(name), stdin);  // read string.
  printf("Name: ");
  puts(name);    // display string.
  return 0;
}
Output
Enter name: Rahul kumar
Name: Rahul kumar

I Hope, अब आपको C language में string के बारे में अच्छे से समझ आ गया होगा।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers