Strings in C

Last Updated :

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.

Comment
Next Article
CPP_Programmer Published 25 Aug, 2020 · 3 min read
Article Tags :

Similar Reads

  • What should we use void main() or int main() ?

    There is question that what should we use void main() or int main() ? void main() { /* ... */ } Or, int main() { /* ... */…

    2 min read
  • C Comments

    Prerequisite – C Language Introduction C comments explain code and improve readability. These do not affect program execution. You can use comments in C to clarify code and describe…

    1 min read
  • C Programming Language Standard

    Prerequisite – C Language Introduction The C programming language has various versions: C89/C90, C99, C11, and C18. C89/C90: Released in 1989/1990. It introduced key language features. C99: This…

    2 min read
  • Features of C Programming Language

    Prerequisite – C Language Introduction C is a simple language made in 1972 by Dennis Ritchie. It’s for system programming. C has low-level memory access, basic keywords, good…

    1 min read
  • C Language Introduction

    C is a programming language developed by Dennis Ritchie in 1972. It has a significant historical impact. It was originally created at Bell Laboratories of AT&T Labs for…

    4 min read
  • Code editors

    Code editors are where programmers spend most of their time. There are two main types of code editors: IDEs and Lightweight editors. IDEs (Integrated Development Environments) IDEs (Integrated…

    1 min read