Introducing C String

Learn how to declare, initialize, and handle the strings.

CString

A string, in computer science, is a sequence of characters. Almost all the programming languages implement strings in different ways.

In C language, we call it the cstring, which is stored in an array of characters ending with '\0' or NULL (also called the null character, which has the ASCII value of 0). The '\0' character marks the end of a string.

Strings are always enclosed in double quotes, while characters are always enclosed in single quotes.

Look at the following examples of strings:

Press + to interact
#include<iostream>
using namespace std;
int main()
{
char a[8] = {'C','+','+'}; // a[] = {'C','+','+','\0','\0','\0','\0','\0'};
char b[8] = "C++"; // b[] = {'C','+','+','\0','\0','\0','\0','\0'};
char c[] = "C++"; // c[] = {'C','+','+','\0'};
char astring[]="Hello world";
cout << "Before Updating..."<<endl;
cout << "a[]: "<<a<<endl; // We will discuss below how this print is working.
cout << "b[]: "<<b<<endl;
cout << "c[]: "<<c<<endl;
cout << "string[]: "<<astring<<endl;
a[0] = 'A';
b[0] = 'B';
c[0] = 'D';
cout << "\nAfter Updating..."<<endl;
cout << "a[]: "<<a<<endl; // We will discuss below how this print is working.
cout << "b[]: "<<b<<endl;
cout << "c[]: "<<c<<endl;
return 0;
}

The first two declarations of the strings a[8] and b[8] are equivalent.

Like in int or float arrays, when we initialize the first few values at the time of declaration, the rest of the values in the array are assigned to 0. Since '\0' has the ASCII value of 0, all the indices followed by the last character will automatically be initialized to 0 or '\0'.

The third declaration, c[], declares an array of exactly size 4, where the last character '\0' is automatically appended in the quoted string.

Similarly, we may also store strings with a space in between characters at initialization time

Note that ' ' is stored as a character with an ASCII value of 32.

For example, we have the following:

char string[]="Hello world";
%0 node_1 'H' node_2 'e' node_3 'l' node_1650955315450 'l' node_1650955264328 'o' node_1650955326409 ' ' node_1650955313833 'w' node_1650955260437 'o' node_1650955282485 'r' node_1650955344920 'l' node_1650955274368 'd' node_1650955286489 '\0'
String declaration

Visually, a character array seems exactly the same as a usual array:

char s[5];  // every element of s[] will be a garbage value.

Strings declaration

1

Are these equivalent statements?

char s[] = "four";
char s[5] = "four";
char s[] = {'f','o','u','r','\0'};
char s[5] = {'f','o','u','r','\0'};
char s[5] = {'f','o','u','r'};
A)

Yes

B)

No

Question 1 of 20 attempted

Arrays and cstring do not support assignment operators. For instance, if we executed the code below, it would give errors.

Press + to interact
/*
The code will give an error, we'll need to use a loop instead
to manually copy values from arr1 to arr2
*/
int arr1[5] = {1, 2, 3, 4, 5};
int arr2[5] = {3, 4, 7, 8, 9};
arr1[] = arr2[];
// Similarly, this will also give an error:
/*
here, we are trying to use the assignment operator to assign the value
"Howdy world" to string, but this is not allowed for cstrings. Instead,
a library function such as strcpy() would need to be used to copy the
new value to the cstring.
*/
char string[] = "Hello world";
char string = “Howdy world”;

Once they are declared, we can re-assign any specific value by using array indexing like s[i]='*'. Or we could update the value using a loop:

Press + to interact
char CharArr1[5] = {'a', 'b', 'c', 'd', \0'};
char CharArr2[5]={}; // Initialize every index to be '\0'
for(int i=0; i<5; i++)
{
CharArr2[i] = CharArr1[i]; // This will reassign s[] = {'A','B', 'C', 'D','E'}
}

Printing strings vs integer arrays

When writing programs, strings are very useful because, for users who are using the software, all the communications through the console are happening through messages and text. This null-terminating character array is quite helpful.

In contrast to integer arrays (remember whenever an int/float array name is asked to display, the stream insertion operator prints the base address of the array), when a character array’s name is asked to print, it displays all the contiguous regions character by character, until it reaches the null-terminating character.

Look at the following example:

Press + to interact
#include <iostream>
using namespace std;
int main() {
char str[] = "Hello World";
int A [] = {1,2,3,4,5};
cout << "str: "<<str<<endl; // this displays "Hello World"
cout << "A: "<<A<<endl; // this displays the address of A or &A[0]
cout << "A+1: "<<A+1<<endl; // this displays the address of A or &A[1]
cout << "A+2: "<<A+2<<endl; // this displays the address of A or &A[2]
cout << "A+3: "<<&A[3]<<endl; // this displays the address of A or &A[3]
cout << "str: "<<str<<endl; // this displays "Hello World"
cout << "str+1: "<<str+1<<endl; // this displays "ello World"
cout << "str+2: "<<str+2+2<<endl;// this displays "llo World"
cout << "str+3: "<<&str[3]<<endl;// this displays ""lo World""
return 0;
}

Notice, whenever cout is asked to print any character address, it considers the address as a cstring (null-terminating character array) and starts printing character by character until it reaches '\0'.