String Constants vs Char Arrays in C

A string constant is a series of characters(array of characters or group of characters) enclosed with double quotes(like “GOOD “). A string constant is a one-dimensional (str11[]) array of characters. A character constant is a character enclosed in single quotes. (like ‘G ‘). Each character in a string takes only 1 byte.

String and Character Array in C

#include<stdio.h>

#include<conio.h>

void main()

{

char str11[]={‘X’,’C’,’N’,’O’,’T’,’E’,’S’,’\0′};//unsized character

char str22[]=”I Love Programming”;

int ii;

clrscr();

/*print out str11*/

for(ii=0;str11[ii];ii++)

printf(“%c”,str11[ii]);

printf(“\n”);

/*print out str22*/

for(ii=0;str22[ii];ii++)

printf(“%c”,str22[ii]);

getch();

}

String Array and String Constant in C

Explanation…

In this example, there are two character arrays str11 and str22.

In the declaration of str11, a set of character constants, including a null character(‘\0’), is used to initialize the array.

For str22, a string constant is assigned to the array.

The compiler will append a null character to str22.

Both str11 and str22 are unsized arrays means the compiler will automatically figure out how much memory is needed.

The for loop prints out all the elements in str11. After the execution of the for loop, the string constant is shown on the screen.

Another for loop displays the string constant assigned to str22 by putting every element of the array on the screen.