C'Language - [String]

♠ Posted by Unknown in at 00:51

Character Array:

A string is an array of characters. A string in ‘C’ is defined as any group of characters enclosed between double marks. Double quotation marks do not form part of the character string.

A string in ‘C’ is stored internally in character arrays. Since the string can be of any length, the end of the string is marked with the single character ‘\0’, the “null” character, which has the ASCII value zero.

Syntax :
                             <char> <array-name> [<size>]

Consider, we want to store the string “VAKRATUND”. For this purpose, we need to define a character array of 10 elements.

The string will be stored in the character array as shown below.

Image: Character Array

The fact that the string must be terminated by a null character means that we must define the array that is going to hold a string to be one byte larger than the largest string it will be required to hold, to make room for the null.

Initialization of String :

String can be initialized at the time of declaration or using for loop in the program.
At the time of declaration, the string can be initialized by

1.     Specifying the group of character in double quote marks (“ “).

2.     Specifying the group characters in open and close braces {}, included in single quotes and separated by commas.

Example:

1.     char name_array [10] = “VAKRATUND”;

2.     char name_array [10] = {‘V’,’A’,’K’,’R’,’A’,’T’,’U’,’N’,’D’,’\0’};

0 comments:

Post a Comment