Jumat, 20 Desember 2013

About and Uses of String Data Types in Computer Programming: C

String is a data type and is often used as an array of bytes that stores a sequence of elements i.e. characters, in computer programming. C language supports string in every calculation of programming, the article describes the use with examples.

No program generally exists without the manipulation of the strings. In all our previous articles, we have used several times the statements:
pritnf("Enter the number of n");

Here, the sequence of characters enclosed within a pair of double quotes is a string. Strings are generally used to store and manipulate data in text form like words or sentences.

Definition: An array (or sequence) of characters is called as string. A string is most conveniently represented using continuous storage locations in memory. The storage representations of strings are classified as shown below:

Fixed length Format:

A fixed length string is a string whose storage requirement is "known when the string is created". In this string, irrespective number of characters the string has, the length of the string is always fixed. Once the string is defined, the length of a string cannot be changed. It is ‘fixed’ for the duration of program execution. In this format, the non-data characters such as space are added at the end of the data.

For example, the string "DOT" and "PROGRAM" are stored in fixed format where size is fixed for 10 bytes as shown below:


Note: The string "DOT" is 10 bytes string ending with 7 blanks characters.
Note: The string "PROGRAM" is 10 bytes string ending with 3 blanks characters.
Note: The blank characters cannot be used as data since it acts as end of the string.

Disadvantages
  • If the length reserved for string is too small, it is not possible to store the large string.
  • If the length reserved for string is too large, too much memory is wasted.
  • Once the string is defined, the length of a string cannot be changed. It id ‘fixed’ for the duration of program execution.
  • There is the problem of distinguishing data character from non-data characters. Normally non-data character such as spaces are added at the end of the data.
The above disadvantages can be overcome using variable length format, will be discussed in later article.

String literals and constants

Tidak ada komentar:

Posting Komentar