C Strings

In c, string is a sequence of characters, terminated by \0 character. For Example "C language" is a string. In c, string can contain character, digit or special symbol enclosed inside double quotes.

\0 is called NULL CHARACTER, It represents end of a string in c language.

Example

void main(){
	char language[] = "C language";
        printf("%s", language);
}

In above example, Each character is stored in array index like

language[0] = 'C' 
language[1] = ' ' 
language[2] = 'l' 
language[3] = 'a' 
language[4] = 'n'
language[5] = 'g'
language[6] = 'u'
language[7] = 'a'
language[8] = 'g'
language[9] = 'e'
  1. %s format specifier is used to output string value in c.
  2. char data type is used for strings, because string is collection of characters.
  3. String in c, can be output directly using %s or access single character by character using loop with %c access modifier.

String with single characters in array

In C, while creating a string with character in an array, It must end with \0 null character.

#include <stdio.h>

int main () {
   char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
   printf("Greeting message: %s\n", greeting );
   return 0;
}

Access string using loop

void main(){
 char language[] = "C language";
 for(int i = 0; i < 11; i++){
      printf("%c\n", language[i]);
 }
}
Output
C language

In above example, loop is used to output string, Loop is accessing each individual index from 0 to 11, and output each single character using %c access specifier.