C Programming Strings

In C programming, a string is a sequence of characters terminated with a null character \0. For example:

  1. char c[] = "c string";

When the compiler encounters a sequence of characters enclosed in the double quotation marks, it appends a null character \0 at the end by default.

Memory diagram of strings in C programming

How to declare a string?

Here’s how you can declare strings:

  1. char s[5];

string declaration in C programming

Here, we have declared a string of 5 characters.

How to initialize strings?

You can initialize strings in a number of ways.

  1. char c[] = "abcd";
  2. char c[50] = "abcd";
  3. char c[] = {'a', 'b', 'c', 'd', '\0'};
  4. char c[5] = {'a', 'b', 'c', 'd', '\0'};

Initialization of strings in C programming

Let’s take another example:

  1. char c[5] = "abcde";

Here, we are trying to assign 6 characters (the last character is '\0') to a char array having 5 characters. This is bad and you should never do this.

Read String from the user

You can use the scanf() function to read a string.

The scanf() function reads the sequence of characters until it encounters whitespace (space, newline, tab etc.).

Example 1: scanf() to read a string

  1. #include <stdio.h>
  2. int main()
  3. {
  4. char name[20];
  5. printf("Enter name: ");
  6. scanf("%s", name);
  7. printf("Your name is %s.", name);
  8. return 0;
  9. }


Enter name: Dennis Ritchie
Your name is Dennis.

Even though Dennis Ritchie was entered in the above program, only “Ritchie” was stored in the name string. It’s because there was a space after Dennis.

How to read a line of text?

You can use the fgets() function to read a line of string. And, you can use puts() to display the string.

Example 2: fgets() and puts()

  1. #include <stdio.h>
  2. int main()
  3. {
  4. char name[30];
  5. printf("Enter name: ");
  6. fgets(name, sizeof(name), stdin); // read string
  7. printf("Name: ");
  8. puts(name); // display string
  9. return 0;
  10. }


Enter name: Tom Hanks
Name: Tom Hanks

Here, we have used fgets() function to read a string from the user.

fgets(name, sizeof(name), stdlin); // read string

The sizeof(name) results to 30. Hence, we can take a maximum of 30 characters as input which is the size of the name string.

To print the string, we have used puts(name);.

Note: The gets() function can also be to take input from the user. However, it is removed from the C standard.

It’s because gets() allows you to input any length of characters. Hence, there might be a buffer overflow.

Passing Strings to Functions

Strings can be passed to a function in a similar way as arrays. Learn more about passing arrays to a function.

Example 3: Passing string to a Function

  1. #include <stdio.h>
  2. void displayString(char str[]);
  3. int main()
  4. {
  5. char str[50];
  6. printf("Enter string: ");
  7. fgets(str, sizeof(str), stdin);
  8. displayString(str); // Passing string to a function.
  9. return 0;
  10. }
  11. void displayString(char str[])
  12. {
  13. printf("String Output: ");
  14. puts(str);
  15. }

Strings and Pointers

Similar like arrays, string names are “decayed” to pointers. Hence, you can use pointers to manipulate elements of the string. We recommended you to check C Arrays and Pointersbefore you check this example.

Example 4: Strings and Pointers

  1. #include <stdio.h>
  2. int main(void) {
  3. char name[] = "Harry Potter";
  4. printf("%c", *name); // Output: H
  5. printf("%c", *(name+1)); // Output: a
  6. printf("%c", *(name+7)); // Output: o
  7. char *namePtr;
  8. namePtr = name;
  9. printf("%c", *namePtr); // Output: H
  10. printf("%c", *(namePtr+1)); // Output: a
  11. printf("%c", *(namePtr+7)); // Output: o
  12. }

Commonly Used String Functions

Leave a Reply

Your email address will not be published. Required fields are marked *