C String : Introduction

In simple words a string is nothing but the sequence of characters or symbols between a pair of double-quote characters, that is treated as a single data item. For example :
 "This is the String.!"
 "Hello world 1,2,3"
In C language strings are represented by one-dimensional array of characters terminated by a null character '\0', where null character indicates the end of the string. Also note that strings are always enclosed by double quotes. For example the below statement create a string consisting of word "mycsnotes".
char str[] = "mycsnotes";
And the null character will be implemented at end of the string by the compiler. Now the string array str will look like :


Now lets see some example of strings in C language.

Example :
#include <stdio.h>

int main() {
  char str[] = "Hello world, from mycsnotes";
  printf("%s\n", str);
  return 0;
}
Output :

Hello world, from mycsnotes

We can also initialize character by character, for example :
char str[] = {'m', 'y', 'c', 's', 'n', 'o', 't', 'e', 's', '\0'};
Example 2 :
#include <stdio.h>

int main() {
  char str[] = {'m', 'y', 'c', 's', 'n', 'o', 't', 'e', 's', '\0'};
  printf("%s\n", str);
  return 0;
}
Output :

mycsnotes

We can also declare string with providing the size of string.
char str[20] = "Hello world";
Where the size of the 11 bytes of space are filled with provided value,1 byte for null character, and last 8 bytes are remains empty.

Example :
#include <stdio.h>

int main() {
  char str[20] = "Hello world";
  printf("%s\n", str);
  return 0;
}
Output :

Hello world


Next Topic :