C Programming Strings

In this article, you'll learn to handle strings and its operations in C. You'll learn to declare them, initialize them and use them for various input/output operations.
C strings

In C programming, array of characters is called a string. A string is terminated by a null character /0. For example:

"c string tutorial"

Here, "c string tutorial" is a string. When, compiler encounter strings, it appends a null character /0 at the end of string.

Memory diagram of strings in C programming

Declaration of strings

Before we actually work with strings, we need to declare them first.

Strings are declared in a similar manner as arrays. Only difference is that, strings are of char type.

Using arrays

char s[5];

Declaration of strings in C language

Using pointers

Strings can also be declared using pointer.

char *p;

Initialization of strings

In C, string can be initialized in a number of different ways.

For convenience and ease, both initialization and declaration are done in the same step.

Using arrays

char c[] = "abcd";
     OR,
char c[50] = "abcd";
     OR,
char c[] = {'a', 'b', 'c', 'd', '\0'};
     OR,
char c[5] = {'a', 'b', 'c', 'd', '\0'};

Initialization of strings in C programming

The given string is initialized and stored in the form of arrays as above.

Using pointers

String can also be initialized using pointers as:

char *c = "abcd";

Reading Strings from user

You can use the scanf() function to read a string like any other data types.

However, the scanf() function only takes the first entered word. The function terminates when it encounters a white space (or just space).

Reading words from user

char c[20];
scanf("%s", c);

Example #1: Using scanf() to read a string

Write a C program to illustrate how to read string from terminal.

#include <stdio.h>
int main()
{
    char name[20];
    printf("Enter name: ");
    scanf("%s", name);
    printf("Your name is %s.", name);
    return 0;
}

Output

Enter name: Dennis Ritchie
Your name is Dennis.

Here, program ignores Ritchie because, scanf() function takes only a single string before the white space, i.e. Dennis.

Reading a line of text

An approach to reading a full line of text is to read and store each character one by one.

Example #2: Using getchar() to read a line of text

1. C program to read line of text character by character.

#include <stdio.h>
int main()
{
    char name[30], ch;
    int i = 0;
    printf("Enter name: ");
    while(ch != '\n')    // terminates if user hit enter
    {
        ch = getchar();
        name[i] = ch;
        i++;
    }
    name[i] = '\0';       // inserting null character at end
    printf("Name: %s", name);
    return 0;
}

In the program above, using the function getchar(), ch gets a single character from the user each time.

This process is repeated until the user enters return (enter key). Finally, the null character is inserted at the end to make it a string.

This process to take string is tedious.

Example #3: Using standard library function to read a line of text

2. C program to read line of text using gets() and puts()

To make life easier, there are predefined functions gets() and puts in C language to read and display string respectively.

#include <stdio.h>
int main()
{
    char name[30];
    printf("Enter name: ");
    gets(name);     //Function to read string from user.
    printf("Name: ");
    puts(name);    //Function to display string.
    return 0;
}

Both programs have the same output below:

Output

Enter name: Tom Hanks
Name: Tom Hanks

Passing Strings to Functions

Strings are just char arrays. So, they can be passed to a function in a similar manner as arrays.

Learn more about passing array to a function.

#include <stdio.h>
void displayString(char str[]);

int main()
{
    char str[50];
    printf("Enter string: ");
    gets(str);             
    displayString(str);     // Passing string c to function.    
    return 0;
}
void displayString(char str[]){
    printf("String Output: ");
    puts(str);
}

Here, string c is passed from main() function to user-defined function displayString(). In function declaration, str[] is the formal argument.

String handling functions

There are various string 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>.

Learn more about string manipulations in C.