Strings in C are intertwined with pointers to a large extent. You must become familiar with the pointer concepts covered in the previous articles to use C strings effectively. Once you get used to them, however, you can often perform string manipulations very efficiently.

A string in C is simply an array of characters. The following line declares an array that can hold a string of up to 99 characters.

char str[100];

It holds characters as you would expect: str[0] is the first character of the string, str[1] is the second character, and so on. But why is a 100-element array unable to hold up to 100 characters? Because C uses null-terminated strings, which means that the end of any string is marked by the ASCII value 0 (the null character), which is also represented in C as ‘\0’.

String is a set of characters that are enclosed in double quotes. In the C programming language, strings are created using one dimension array of character datatype. Every string in C programming language is enclosed within double quotes and it is terminated with NULL (\0) character. Whenever c compiler encounters a string value it automatically appends a NULL character (\0) at the end. The formal definition of string is as follows…

String is a set of characters enclosed in double quotation marks. In C programming, the string is a character array of single dimension.

In C programming language, there are two methods to create strings and they are as follows…

  1. Using one dimensional array of character datatype ( static memory allocation )
  2. Using a pointer array of character datatype ( dynamic memory allocation )

Creating string in C programming language

In C, strings are created as a one-dimensional array of character datatype. We can use both static and dynamic memory allocation. When we create a string, the size of the array must be one more than the actual number of characters to be stored. That extra memory block is used to store string termination character NULL (\0). The following declaration stores a string of size 5 characters.

char str[6] ;

The following declaration creates a string variable of a specific size at the time of program execution.

char *str = (char *) malloc(15) ;

Also Read: Control Statements and its Types | C Tutorials

Assigning string value in C programming language

String value is assigned using the following two methods…

  1. At the time of declaration (initialization)
  2. After declaraation

Examples of assigning string value


int main()
{
    char str1[6] = "Hello";
    char str2[] = "Hello!";
    char name1[] = {'s','m','a','r','t'};
    char name2[6] = {'s','m','a','r','t'};

    char title[20];
    *title = "technology learning blogs";
    
    return 0;
}

Reading string value from user in C programming language

We can read a string value from the user during the program execution. We use the following two methods…

  1. Using scanf() method – reads single word
  2. Using gets() method – reads a line of text

Using scanf() method we can read only one word of string. We use %s to represent string in scanf() and printf() methods.

Examples of reading string value using scanf() method

#include<stdio.h>
#include<conio.h>

int main(){

   char name[50];
   printf("Please enter your name : ");
   
   scanf("%s", name);
   
   printf("Hello! %s , welcome to technology learning blogs !!", name);
   
   return 0;
}

When we want to read multiple words or a line of text, we use a pre-defined method gets(). The gets() method terminates the reading of text with Enter character.


Also Read: How to use If-Else Statements in the C Programming Language

Examples of reading string value using gets() method

#include<stdio.h>
#include<conio.h>

int main(){

   char name[50];
   printf("Please enter your name : ");
   
   gets(name);
   
   printf("Hello! %s , technology learning blogs !!", name);
   
   return 0;
}

C Programming language provides a set of pre-definied functions called String Handling Functions to work with string values. All the string handling functions are defined in a header file called string.h.

However, if you want any information related to this post or related to programming language, or computer science, then comment below I will clear your all doubts.

If you want a complete tutorial of C language, then see here C Tutorial. Here you will get all the topics of the C Programming Tutorial step by step.

Friends, if you liked this post, then definitely share this post with your friends so that they can get information about the Strings in C Language.

Thanks for Reading !! Keep Learning !!