0% found this document useful (0 votes)
3 views1 page

Strings

C-style strings are character arrays terminated by a null character '\0'. When declaring these arrays, it's important to account for the null character in the size. Examples of valid C-style string declarations and initializations are provided.

Uploaded by

Ahmad Raza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views1 page

Strings

C-style strings are character arrays terminated by a null character '\0'. When declaring these arrays, it's important to account for the null character in the size. Examples of valid C-style string declarations and initializations are provided.

Uploaded by

Ahmad Raza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

Character Arrays (C-style strings)

The c-style string is a character array terminated with a null character “\0”.
Hence we can define an array of type char and the dimensions or size of the arrays
and then we can initialize it to a string or array of characters.

Note that it’s the compiler that terminates a string with a null character, so if
we initialize the character array with a string (in the double quote “”) then we
need to leave extra space for the null character while declaring the size of an
array.

Let’s take some Examples of declaring and initializing character arrays.

char firstStr[] = "This is Cstyle string";


char secStr[100] = {'s','o','f','t','w','a','r','e','
','t','e','s','t','i','n','g',' ','h','e','l','p','\0'};
char thirdStr[] = {'h','e','l','l','o','\0'};
All the above definitions are valid C-style string definitions in C++. Note that we
can either declare the actual size or we can leave the size blank so that the
compiler can accommodate the size depending on the string that we initialized.

You might also like