Home »
C/C++ Zone » C-Array & String
Introduction
A string is a sequence of characters that is treated as a single data item. We have used strings in a number of examples in a number of example in the past. Any group of characters (except double quote sign) defined between double quotation marks is a string constant. Example:
"Man is obviously made to think."
For example,
printf ("\" Well Done !"\");
will output the string
" Well Done !"
While the statement
printf (" Well Done !");
will output the string
Well Done!
printf ("\" Well Done !"\");
will output the string
" Well Done !"
While the statement
printf (" Well Done !");
will output the string
Well Done!
1. List of temperatures recorded every hour in a day, or a month, or a year
2. List of employees in an organization.
3. List of products and their cost sold by a store.
4. Table of daily rainfall data and so on.
Character strings are often used to build meaningful and readable programs. The common operations performed on character string include:
1. Reading and writing strings
2. Combining string together
3. Copying one string to another
4. Comparing string for equality
5. Extracting a portion of a string
In this chapter we shall
discuss these operations in detail and examine library function
that implement them.
Declaring and Initializing String Variable
C does not support strings as a data type. However, it allows us to represent string as character arrays. in c therefore, a string variable is any valid variable name and is always declared as an array of characters. The general form of declaration of a string variable is.
char string_name[ size ];
The size determine the number of characters in the string....
name. Some examples are:
char city[10];
char name[30];
char name[30];
Like numeric arrays, character arrays may be initialized when they are declared C permits a character array to be initialized in either of the following two forms
char city [9] = " NEW YORK ";
char city [9] = {' N ',' E ',' W ',' ' ,' Y ',' O ',' R ',' K '};
This will result in a compile time error. Also note that we cannot separate the initialization from declaration. This ischar city [9] = {' N ',' E ',' W ',' ' ,' Y ',' O ',' R ',' K '};
char str3[5];
str3 = "GOOD";
is not allowed. Similarly,str3 = "GOOD";
char s1[4] = "abc";
char s2[4];
s2 = s1; /* Error //
is not allowed. An array name cannot be used as the left operand
of an assignment operator.
char s2[4];
s2 = s1; /* Error //

