Search Your Topic

Thursday, 22 June 2017 03:00

Strings in C

Written by
Rate this item
(0 votes)

String in C language is an array of characters that is terminated by \0 (null character).

There are two ways to declare string in c language.

  1. By char array
  2. By string literal

Let's see the example of declaring string by char array in C language.

  1. char ch[10]={'t''o''s''h''i''y''a''s''i''t''\0'};  


While declaring string, size is not mandatory. So you can write the above code as given below:

  1. char ch[]={'t''o''s''h''i''y''a''s''i''t''\0'};  

You can also define string by string literal in C language. For example:

  1. char ch[]="toshiyasit";  

In such case, '\0' will be appended at the end of string by the compiler.

Difference between char array and string literal

The only difference is that string literal cannot be changed whereas string declared by char array can be changed.


String Example in C

Let's see a simple example to declare and print string. The '%s' is used to print string in c language.

  1. #include <stdio.h>  
  2. void main ()  
  3. {  
  4.    char ch[11]={'t''o''s''h''i''y''a''s''i''t''\0'};  
  5.    char ch2[11]="toshiyasit";  
  7.    printf("Char Array Value is: %s\n", ch);  
  8.    printf("String Literal Value is: %s\n", ch2);  
  9. }  


Char Array Value is: toshiyasit
String Literal Value is: toshiyasit
Read 5548 times
Anish Sir

"I am delighted once again to pen the welcome note to the Tosh!Yas Technologies ."

 Call +91 74 88 34 7779  | Email :

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.