Linux C Programming Tutorial Part 9 : Strings

In this ongoing C programming tutorial series, we have already touched upon the concept of character arrays. Closely related to character arrays is the concept of strings, which we'll be discussing here.

A string differs from a character array in one major area - it is terminated by a Null character '\0'. There is no such terminating character in a charcter array. The following piece of code shows some ways in which a string can be defined:

#include <stdio.h>

int main()
{
char str[] = "string";
char str1[] = {'s','t','r','i','n','g','\0'};
char str2[10] = "string";
char str3[10] = {'s','t','r','i','n','g','\0'};
char *str4 = "string";

printf("\n str=%s, str1=%s, str2=%s, str3=%s, and str4=%s", str, str1, str2, str3, str4);

return 0;
}

So, a bunch of characters in double quotes is a string (terminating Null character is implicit there), and a series of characters explicitly ending with a \0 character is also a string.

The output of this program is:

 str=string, str1=string, str2=string, str3=string, and str4=string

Keep in mind you can't print non-string character arrays this way, as there is no terminating Null charcter there to signify end of the array. Here's a piece of code that tries to do this:

#include <stdio.h>

int main()
{
char str[] = {'e','n','d'};
char c ='t';

printf("\n str=%s", str);

return 0;
}

But ends up printing garbage values at the end instead:

str=end??

Moving on, just like the %s format specifier in 'Printf' tells compiler to treat input variables as strings, you can also use %s in 'scanf' function to accept strings as input from user. Here's an example:

#include <stdio.h>

int main()
{
char str[30];

printf("\n Enter a string with length less than 30 characters: ");
scanf("%s",str);

printf("\n str=%s", str);

return 0;
}

It's worth mentioning that a sequence of zero or more character surrounded by double quotes is called a string constant, or string literal. This means both "howtoforge" and "" are string constants or string literals.

You can find length of a string using the standard strlen() function. Here's an example:

#include <stdio.h>

int main()
{
char str[] = "howtoforge";

printf("\n Length of string 'howtoforge' is: %d", strlen(str));

return 0;
}

The output produced by this program is 10, which is exactly the number of characters in 'howtoforge', and hence its length.

Moving on, it's worth keeping in mind that a character in single quotes (like 'd') and a character in double quotes (like "d") are different from each other.  While the first one is a character constant (which can be used to produce numeric value of the character in the machine's character set), the second one is a string (meaning there's a terminating \0 character in it). 

Finally, let's quickly take a look at how strings can be passed as arguments to functions. 

#include <stdio.h>

void change_value(char s[])
{
s[0] = 'H';
s[5] = 'F';

printf("%s", s);

}

int main()
{
char str[] = "howtoforge";

change_value(str);

return 0;
}

So in the program above, it's the name of the array 'str' that's passed as argument because it refers to the base address of the array. The declaration for function 'change_value' is such that it expects an array as input. Here's the output of this program:

HowtoForge                                                                                                        

Conclusion

So in this tutorial, we discussed the basics of strings, including how they are defined and how they can be used. We'll learn more about strings in tutorials to come but this article should be enough to get you started. Let us know in comments below if you have any query or doubt related to strings.

Share this page:

Suggested articles

1 Comment(s)

Add comment

Comments

By: David Webb at: 2019-03-12 19:32:46

Shouldn't this tutorial be Part 10? There is a link on right side of page: C Programming Tutorial Part 9 - Strings