The perfect place for easy learning...

C Programming Language

×

Topics List

Place your ad here

Strings in C

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) ;

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 = "btech smart class";
    
    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 btech smart class !!", 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.

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 , welcome to btech smart class !!", 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.


Place your ad here
Place your ad here