/    /  STRINGS

STRINGS

 

Introduction to Strings

 

  • A character array represents a string that ends with a Null character (\0).
  • Null character is an escape sequence with ASCII value 0.
  • Null character is placed automatically at end of the string.

 

Declaration of string

 

Syntax:

 

char varname[size];

 

Example:

 

char A[10];

 

A is a character array of size 10.

 

Initialization of string

 

  • Like variables, strings are also initialized in 2 ways.
  • Static initialization/compile time
  • Dynamic initialization/Run time
  • Static initialization is done in two ways:

(i) Assigning a string to a character array.

(ii) Assigning character by character to a character array.

 

Case 1:

 

char s[10]=”Hello world”;

 

The compiler automatically inserts the Null character at the end.

 

Case 2:

 

char s[10]= {'H','e','l','l','o',','w','o','r','l','d','\0'};

 

If data is given character by character ‘\0’ is added manually.