C and C++ String data type

The C and C++ string type can represent a sentence or a lines of text.A char type variable can hold only a single letter at a time but the string type can hold many characters ;this is one of the difference between string and char data type.

In C the string is a built-in data type whereas in C++ the string is not a built-in data type.It is a different data type known as class.More about class will be discussed in another chapter.Note the C++ programmer can also use the C type string in their programs.To make the discussion on string easier let us examine the C string and C++ string differently.

i)C string
ii)C++ string

C string

To use the C string we will use the ‘char’ data type but with little modification.The syntax is shown below.

char string[]; 

The ‘[]‘ after the variable name signify that it is of string type.So now we can assign a sentence to it.Look at the code below.

char string[]="A sentence"; //note the double quotation here 

char ss[]="234 new" ; //work fine

char s="c"; //work fine

/*char c="Plane"; definitely an error! */

printf("%s \n %s", string , s);

/******For C++ programmer ********/
cout<< string << endl << ss ;
/*********************************/

The output, is,

A sentence
234 new
 

As you can see assigning a sentence to char type is not allowed but we can assign a character to a string type.

The C string has no definite size.The number of characters hold by the string is the size of the string.Consider the code below.

char string[]="New happy";

char ss[]="New"

printf( "%d  %d" ,sizeof(string) ,sizeof(ss) );

/*******For C++ programmer********/
cout<< sizeof(string) << "  " << sizeof(ss) ;
/*********************************/

The output is
10 4
 
10 for “New happy” and 4 for “New”.

If you count the number of character in a sentence “New happy” you will get only 9 why is the output 10.This is because the at the end of the sentence a character known as new line(written as ‘\0’) is attached,this is done automatically by the compile.So the actual sentence is “New happy\0” the ‘\0’ character is kept hidden.This also explains why the size of ss(“New”) string is also 4.


C++ string

The C++ string keyword is ‘string‘ and unlike the C string the size doe not vary but rather it is fixed which is 4 bytes( 32 bits).Look at the code below.

string s="A sentence" , ss="New"

cout<< s << endl; //print out the s string

cout<< "The size of s and ss string are=" << sizeof(s) << " " << sizeof(s) ;

The output is,

A sentence
The size of s and ss string are= 32 32
 

Here for both the string s and ss the output of the size of s and ss are 32.Here 32 means 32 bits which equals to 4 bytes,so 4 bytes is the size of every string.

Note the concept of string in C and C++ are different.One of the differences is:security,C++ string type is more secure than C string type.While using string in our program,C++ string type is always recommended.We will have more detail discussion on ‘string’ in Chapter 7.


Uses of string type

Here we will see some uses of string type.

i) A string variable can hold many characters including numbers,special letters,escape sequence,etc. So if you want to make a string that can hold both characters and numbers combine ,say for instance you want to represent a date with slashes between a date ,month and year then string is the perfect choice.Consider the code below.

string date="Date:4/1/2016" ;

printf("%s" , date);

/****** For C++ prgrammer***/ 
cout<< date ;
/***************************/

Output,

Date:4/1/2016


ii) Another uses of string is to represent a real number value with very large precision and accuracy.We know floating point type can represent a real number,but it can represent maximum only up to six decimal point value.If you command the compiler to output more than six decimal point using a standard function,it will but it won’t be accurate.

double d=123.48763567328832973939823623342 ;

cout<< d << endl
  << setprecision(25) << endl; //forcing compiler to output up to 25 precision value

Output,

123.488
123.487635673288323800989

After the 14th decimal point digit the value loses it’s accuracy.This can create a grievous situation in Mathematics calculation requiring high accurate value.So a better solution is to use string to represent such value.

string value=”123.48763567328832973939823623342″ ;

cout<< value ; 

Now the value will not lost it’s accuracy even up to say 200 precision value.