Char Array to String – Programming in ANSI C Language

A string is a sequence of characters that is treated a single data item Char array to string. We have used string in a number of examples in the past. Any group of char (characters) defined between double quotation marks is a string constant. Ex : “Man is obviously made to think”.

If we want to include a double quote in the string to be printed, then we may use it with a back slash as shown below.

“\” Man is obviously made to think,\” said Pascal.”

for example, : printf (“\” Well Done !”\”);

will output the string  : “Well Done !”

While the statement : printf(“Well Done !”);

Will output the string : Well Done !

Character strings are often used build meaningful and readable programs. The common operations performed on character strings include.

  • Reading and writing strings.
  • Combining strings together.
  • Extracting a portion of a string.
  • Copying string to another.
  • Comparing strings for equality.

In this chapter we shall discuss these operations in detail and examine libary functions that implement them.

Declaring and Initialing String Variables

C language does not support strings as a data type. However, it allows us to represent strings as character arrays. In C language, therefore, a string variable is any C language variable name and is always declared as an array of characters. The general form of declaration of string variable is : char string_name[size];

The size determines the number of character in the string_name.

char city[10];
char name[30];

When the compiler assigns a character string to a character array, it automatically supplies a null character (‘\0’) at the end of the string. Therefore, the size should be equal to the maximum number of character in the string plus one. Like numeric arrays, character arrays may be initialized when they are declared. C language permits a char array to string 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’,’\O’};

The reason that city had to be 9 elements long is that the string NEW YORK contains 8 characters and one element space is provided for the null terminator. Note that when we initialize a character array by listing its elements, we must supply explicity the numm terminator.

C language also permits us to initialize a character array without specifying the number of elements. In such cases, the size of the array will be determined automined automatically, based on the number of elements initialized, for Example, the statement : char string [] = {‘G’,’O’,’O’,’D’,’\0};

defines the array string as a five element array. We can also declar size much longer than the string size in the initializer. That is statement, char str[10] = “GOOD”; is permitted. In this case, the computer creater array of size 10, places the value “GOOD” in it, terminates with the null character, and initializes all other elements to NULL.

G O O D \0 \0 \0 \0 \0 \0

However, the following declaration is illegal. char str2[3] = “GOOD”;

This will result in a compile time error. Also note that we cannot separate the initialization from declaration.

char str3[5];
str3 = “GOOD”;

is not allowed, similarly,

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.

Terminating Null Character – Char Array to String

You must wondering, “why do we need a terminating null character?” As we know, a string is not a data type in C, but it is considered a data structure stored in an array. The string is a variable length structure and is stored in a fixed length array. The array size is not always the size of the string and most often it is much larger than the string stored in it. Therefore, Char array to string the last element of the array need not represent the end of the string. We need some way to determine the end of the string data and the null character serves as the “end-of-string” marker.

Read More Topics
Functions of Layer in the OSI Model
Decision Making and Branching in C
Software Testing and Quality Assurance
Importance of C and Programming in ANSI C
The Open System Interconnection (OSI) Model
Glossary for Computer Network
The Point-to-Point Protocol (PPP)

About the author

Santhakumar Raja

Hi, This blog is dedicated to students to stay update in the education industry. Motivates students to become better readers and writers.

View all posts

Leave a Reply