C - String

C Programming

Share

In this tutorial we will learn about strings in C programming language.

What is a string?

A string is a sequence of characters enclosed in double quotes.

There is no string data type in C.

To store string values in C we create an array of type char.

Syntax of a character array to store string value is given below.

char strName[size];

Where, strName is the name of the variable and size tells us about the number of characters the array will hold.

Strings end with a NULL \0 character.

If we want to store a string like "Hello" having 5 characters then we will need a character array of size (5 + 1) i.e., 6 as the last character will be the NULL character.

Creating and initialising string variables

In the following example we are creating a string variable which is a character array of size 6 and assigning "Hello" to it.

char str[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

We can skip the size and C will determine the size based on the number of elements in the array.

char str[] = {'H', 'e', 'l', 'l', 'o', '\0'};

We can also assign the value in the following way.

char str[6] = "Hello";

All the methods shown above will create an array str of size 6 and can be represented as follows.



If the size is greater than the number of characters then in that case rest of the spaces in the array is filled with NULL \0 character.

char str[10] = "Hello";

And it can be represented as follows.



String input using scanf function

We can use the scanf() function to take string input from the user.

In the following example we are taking a string input from the user and saving it in str variable.

char str[100];
scanf("%s", str);

Points to note when using scanf function to take string input.

  • We don't have to use the & character before variable name.
    scanf("%s", varName);    //this is correct
    scanf("%s", &varName);   //this is wrong, & is not required
    
  • The scanf function will automatically add the NULL \0 character at the end of the string. So, we don't have to worry about it. But make sure that the character array size is big enough to hold the input string and the NULL character.
  • The scanf function will terminate user input as soon as it encounters a white space. So, if the user input is "Hello World" then only "Hello" will be saved.

Specifying field width for scanf

We specify the field width when we want to read specific number of characters.

In the following example we have a character array of size 10 and we are going to read only 4 characters.

char str[10];
scanf("%4s", str);

In the above code %4s specify that we are restricting input to 4 characters.

Two things can happen in the above scenario.

  • If the field width is greater than the input string then rest of the space will be filled with NULL character.
  • If the field width is smaller than the input string then extra characters will be truncated.

String input using gets function

We use the gets method to take string input from the user without worrying about white spaces.

The gets() function is available in the stdio.h header file.

In the following code we are taking user input using the gets function.

char str[100];
gets(str);

String output using printf function

We can use the printf() function to output string.

In the following code we are printing out "Hello World" string.

char str[100] = "Hello World";
printf("%s", str);

String output using puts function

We can also use the puts() function to print out the string.

The puts() function is available in the stdio.h header file.

In the following code we are printing out "We are learning C programming language." string.

char str[100] = "We are learning C programming language.";
puts(str);
Share