Strings in C

By | August 25, 2020

In this article, you’ll learn about strings in C programming.

Strings are defined as an array of characters. The difference between a character array and a string is the string is terminated with a special character ‘\0’.

Example :

char name[] = "first name" ;

Declaration of strings :

When the compiler encounters a sequence of characters enclosed in the double quotation marks, it appends a null character \0 at the end by default.

There are two ways to declare a string in c language.

  1. You can declare string “first name” using string literal
    char name[] = "first name" ;
  2. You can also declare this using char array:
    char name[] = {'f', 'i', 'r', 's', 't', ' ', 'n', 
                                   'a', 'm', 'e', '\0'} ; 

Nate that you can also take the address of string “first name” as pointer.

char *name = "first name" ;
// program for string examples
#include <stdio.h>

int main () {

   char name[] = "first name";
   
   // this is same as 
   // char name[] = {'f', 'i', 'r', 's', 't', ' ', 
                               'n', 'a', 'm', 'e', '\0'} ; 
   
   
   // this string is also accessed by pointer
   // char *name = "first name";
   
   // print value of string name
   printf("%s\n", name );
   
   // successful completion 
   return 0;
}

Initializing a String :

Strings do not support the assignment operator once it is declared. For example,

char name[100];
name = "first name";  // Error! array type is not assignable. 

But, you can use the scanf() function to read a string. scanf() function reads the sequence of characters until it encounters whitespace (space, newline, tab, etc.).

// program for take string input
#include <stdio.h>

int main()
{
    // declare size of char array
    char name[20];
    
    printf("name: ");
    // user character input
    scanf("%s", name);
    
    // print user output
    printf("Your name is : %s.", name);
    
    // successful completion
    return 0;
}
Input : first name
Output : first

Note –
If you enter “first name”, then it will print only “name”, because of property of scanf() which reads the sequence of characters until it encounters whitespace (space, newline, tab, etc.). It stop reading after first whitespace (space, newline, tab, etc.) encountered.

Commonly Used String Functions :

These are functions are defined in string.h header file of C.

  • strlen(s1)
    It returns the length of string s1.
  • strcpy(s1, s2)
    It copies string s2 into string s1.
  • strcat(s1, s2)
    It concatenates string s2 onto the end of string s1.
  • strcmp(s1, s2)
    It returns 0 if s1 and s2 are the same; less than 0 if s1s2.
  • strchr(s1, ch)
    It returns a pointer to the first occurrence of character ch in string s1.
  • strstr(s1, s2)
    It returns a pointer to the first occurrence of string s2 in string s1.

Important Points :

  1. The name of a string acts as a pointer because it is basically an array.
  2. The C compiler automatically adds a NULL character ‘\0’ to the character array created.
  3. Strings in C are actually arrays of characters.
  4. string.h is collection of functions for string manipulation.
  5. Strings can be passed to a function in a similar way as arrays.
  6. Similar like arrays, string names are “decayed” to pointers. Hence, you can use pointers to manipulate elements of the string.
  7. strlen returns you the length of the string stored in array, however sizeof returns the total allocated size assigned to the array.



Please write comments if you find anything incorrect. A gentle request to share this topic on your social media profile.