Difference between character array and string array

Difference between character array and string array

Difference between character array and string array

In the previous section we have study about array.In this section we are going to study about difference between character array and string array.

Character array:

As we know array is the collection of similar types of data. character array is the collection of characters that may or may not ends with special character called NULL (‘\0’) character.

String array:

String array is the set of characters present within double quotes (” “) and that must ends with special character called NULL (‘\0’)character.

In c++ we can define string array by using the concept of string class,in which there is no need of declaring array size.

Declaration syntax:

Example:

char array[20];

above statement will create array of size 20 bytes having character datatype.

How to initialise character array and string array?

Both can be defined in two ways:

  • at the time of declaration
  • at run time

In the above statement array is initialised at the time of declaration.In character array each character must be quoted in single quote(‘ ‘).Array size is not require when the array is initialised at declaration time.by default its size is same as the size of character array.

numbers or characters present within double quotes are known as string in C.

The difference in the declaration of character array and string array is that in character array elements are present within a single quotes whereas the elements in string array are present within double quotes.

“Therefore go  through the following code snippets which will help you to Difference between character array and string array completely:”

How to read read string at rum time?

OUTPUT:

In the above program I have taken a character array of size 20 bytes.I inserted the character array “arvind” in it and prints the array.Here it is working properly,the problem arises with %s when it encounter a blank space in input array.

Observe the output of program given below carefully you will come to know its demerit.

OUTPUT:

You might have observed the difference in input and output string array.here the input was “arvind kumar” but the output is only “arvind”.Here the thing I want to tell is that whenever you have to take input of a line in such cases don’t use “%s” specifier. “%s” read the entered string until getting first blank space.when a blank space is encountered by the compiler ,simply rest part of the input is discarded.

There are number of ways by using which we can read whole string.

gets()

gets() is a function defined under the header file stdio.h. this function takes complete line as input.

Note: using the gets() function in Linux and ubuntu will produce warning message during compilation.

Specify the escape sequence character :

It is also possible to take the input of string array using scanf. Instead of “%s” format specifier you will have to

write encoutering character/escape sequence character as format specifier whose syntax is given below:

Go through the given code snippets:

OUTPUT:

You might have observed in scanf instead of “%s” I have used scanf(“%[^\n]”,ch) .here the encountering character is “\n” . It will take the input until we fire the enter key from keyboard. in the similar way you can use different character like @,#,$ .after encountering these the compiler will simply ignore the rest part of your string.

You have also seen a new function puts() in the code. By using this function you can print the string. There is no requirement of printf statement and related format specifier.

So finally you came to know “Difference between character array and string array:”

Free online test Powered by Quick Solution.

C Programming Video tutorial by Quick Solution.
Click here to see programs related to this.

Leave a Reply