Strings in C Programming

This tutorial is about strings in C Programming Language. The understanding of strings requires a good knowledge of array. The reason because string is nothing but a one-dimensional array of characters. There is no built-in data type, so we use arrays to work with strings. The simplest example is shown below.

Charcater Array as String
Charcater Array as String

In this example, we have declared character array Word which hold string foobar. This one dimensional array of characters terminates with NULL Character ‘\0’. Our array consists of 6 characters but we have declared Word array of size 7. This is because we always have to use extra character for NULL termination character.

Example: This example code print string of characters “foobar”. The use of character array to print string on screen is easiest method. In further lesson, we will use string library function.

#include <stdio.h>

int main()
   char Word[7] = "foobar";
   printf("%s", Word);
   return 0;

Explanation: The run and build this example code will simply print “foobar” on screen. There is not much to discuss in this code snippet. The only new symbol is %s. This %s is format specifier used for array of char sequence of characters i.e. string. The printf statement simply prints this string on screen.

C Language supports various functions under the <string.h> standard header file. C programmer often find raw string cumbersome to deal with. This is a reason why programmer include #include <string.h> header file in the beginning of code and enjoy built in function. Here are list of functions supported by <string.h> library.

strcpy copy a string
strcat concatenate two strings
strlen get string length
strchr string scanning operation
strcmp compare two strings
strncat concatenate one string with part of another
strncmp compare parts of two strings
strncpy copy part of a string
strrchr string scanning operation

Example: This example uses couple of build in functions to copy a string, concatenate two strings and print the length of given string. The remaining functions and their uses will be covered in future lesson.

#include <stdio.h>
#include <string.h>

int main () {

   char str1[8] = "Binary";
   char str2[8] = "Updates";
   char str3[8];
   int  length;

   strcpy(str3, str1);   // copy str1 into str3
   printf("Copy str1 into str3 :  %s\n", str3 );

   strcat( str1, str2);  // concatenates str1 and str2
   printf("Concatenates Strings:  %s\n", str1 );

   length = strlen(str1);  // total length of str1
   printf("The length of str1 :  %d\n", length);

   return 0;

Let’s build and run this example code and observe output. The output from this program is:

String Standard Functions in C
String Standard Functions in C

Explanation: The first few lines declare three strings str1, str2, str3 and one integer variable length. The strcpy(str3, str1) statement copy content of str1 into str3. So when we call to print str3. We will get Binary printed on a screen. The strcat( str1, str2) statement use to concatenate (link together in series) str1 and str2. This will result in printing BinaryUpdates on screen. The strlen(str1) statement return a length of string str1. Which will be then printed using printf statement.

This is it for Strings in C Programming. We hope you have enjoyed reading this lesson. In next lesson, we’ll learn about Structures, Typedef and Union in C Programming. Please do write us if you have any suggestion/comment or come across any error on this page. Thanks for reading.learn-embedded-system


Get Free Courses & Webinars
You'll receive only high quality learning material, tips & tricks
I agree to have my personal information transfered to MailChimp ( more information )
We respect your privacy

About Umesh Lokhande

Umesh Lokhande holds a Master degree in Scientific Instrumentation from University of Applied Sciences Jena, Germany. and has previously worked at Orbotech, Alere Technologies etc. Umesh is also a founder and first author of BINARYUPDATES.COM


Register | Lost your password?