Strings in C

Strings are groupings of letters, numbers, and many other characters. In C language programmers can create and initialize a string using a character array and a terminating NULL or \0 character.

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

By char array

By string literal

By char Array

Let’s see the example of declaring string by char array in C language.

1

charmyString[5]={'H','e','l','l','o','\0'};

In above example, we have declared an array of characters.

Note that, When we create character arrays, it is important to allocate enough room for the NULL character because many C library functions look for the NULL character when processing character arrays. If the NULL character is not found, some C library functions may not produce the desired result.

By string literal

The variable myString can also be created and initialized with a string literal. String literals are groupings of characters enclosed in quotation marks, as shown next.

1

charmyString[]="Hello";

In above example, we have Assigned a string literal to a character array that creates the necessary a number of memory elements. In this case, five including the NULL character.

Tip: String literals are a series of characters surrounded by double quotes.

You know that strings are arrays of characters in a logical sense, but it’s just as important to know that strings are implemented as a pointer to a segment of memory. More specifically, string names are really just pointers that point to the first character’s memory address in a string.

To demonstrate this thought, consider the following program statement.

1

char*myString="Hello";

This statement declares a pointer variable and assigns the string literal “Hello” to the first and subsequent memory locations that the pointer variable myString points to. In other words, the pointer variable myString points to the first character in the string “Hello”.

Example of String

Let’s see the simple example to declare and print string:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

#include<stdio.h>

intmain()

{

// by using char array

charch[11]={'h','e','l','l','o','w','o','r','l','d','\0'};

// by using string Literal

charch2[13]="beginnersbook";

printf("Char Array Value: %s\n",ch);

printf("String Literal Value: %s\n",ch2);

return0;

}

When you run above program, the output will be following:

1

2

Char Array Value: helloworld

String Literal Value: beginnersbook

Are Strings Data Types?

The concept of a string is sometimes taken for granted in high-level languages such as Visual Basic. This is because many high-level languages implement strings as a data type, just like an integer or double. In fact, you may be thinking or at least hoping—that C contains a string data type as shown next.

1

2

str myString="Hello";//not possible, no such data type

stringmyString="Hello";//not possible, no such data type

C does not identify strings as a data type; rather C strings are simply character arrays.

Reading and Printing Strings

To read and print a character array use the %s conversion specifier as shown in the below program.

1

2

3

4

5

6

7

8

9

10

11

12

#include <stdio.h>

intmain()

{

charcolor[12]={'\0'};

printf("Enter your favorite color: ");

scanf("%s",color);//read char

printf("\nYou entered: %s",color);

return0;

}//end main

When you run above program, the output will be following:

1

2

3

Enter your favorite color: blue

You entered: blue

Above program read the character using scanf() input function and print it using printf() output function.

String handling functions

There are various string manipulation operations you can perform manually like, finding the length of a string, concatenating (joining) two strings etc.

But, for programmer’s ease, many of these library functions are already defined under the header file <string.h>.