C Strings

This post was created and published to provide a description of one of the important topics in the C programming language, which is "string." So without any further delay, let's get started with its brief introduction.

What are strings in C?

A string can be considered a sequence of characters. For example: "fresherearth," "Hello there," "Seattle is a beautiful city," etc. You can also consider the string as an array of characters.

Declare a String in C

Use the following general form to declare a string in the C language:

char name[size];

"char" is the type used to define a character variable; "name" is the name of the string, which is an identifier; and "size" is used to define the number of characters that the character variable "name" can store. For example, consider the following statement:

char str[80];

The above statement tells the compiler that the variable "str" is a character array that can store up to 80 characters.

Note: The null character ('\0') is automatically appended to the end of the string constant by the compiler.

A string constant is a sequence of characters surrounded by double quotes. Here's an illustration.

"Hello there"

Declare and initialize a string in C

To declare and initialize a string in C, use the following general form:

char name[] = "value";

For example,

char str[] = "Hello there";

There is no need to define the "size" if you are going to define the string at the time of declaration. Because the compiler automatically adjusts the size based on the initializing value of the variable.

Use the "%s" format specifier to output a string. For example:

#include<stdio.h>

int main()
{
   char myString[] = "fresherearth dot com";
   printf("%s", myString);

   return 0;
}

The following snapshot shows the output produced by the above program.

c string example

I used the red arrow to draw your attention to the actual output.

You can also use the "%c" format specifier to access the character of the string. Because a string in C is essentially a character array, and as we all know, array indexing always begins with 0, use "mystring[0]" to access the first character of the string and "mystring[strlen(mystring)-1]" to access the last character of the string. Consider the following example as an illustration of this concept:

#include<stdio.h>
#include<string.h>

int main()
{
   char myString[] = "fresherearth dot com";

   printf("The first character = %c", myString[0]);
   printf("\nThe second character = %c", myString[1]);
   printf("\nThe third character = %c", myString[2]);
   printf("\nThe last character = %c", myString[strlen(myString)-1]);
   printf("\nThe second last character = %c", myString[strlen(myString)-2]);
   printf("\nThe third last character = %c", myString[strlen(myString)-3]);

   printf("\n");
   return 0;
}

The output of this example program demonstrating the access of the desired character in a string is shown in the snapshot given below:

c string program

If you want to access a string in a character-by-character manner, then consider the following example:

#include<stdio.h>
#include<string.h>
int main()
{
   char myString[] = "fresherearth dot com";
   for(int i = 0; i < strlen(myString); i++)
   {
      printf("%c", myString[i]);
   }
   printf("\n");
   return 0;
}

The output should be:

fresherearth dot com

A string can also be created in the following ways:

#include<stdio.h>
#include<string.h>
int main()
{
   char str[] = {'H', 'e', 'l', 'l', 'o', ' ', 't', 'h', 'e', 'r', 'e', '\0'};
   printf("%s", str);

   printf("\n");
   return 0;
}

The output should be:

Hello there

Remember to use the null character ('\0') as the last character of the character array; otherwise, the output console will display unexpected results.

C String Example Programs

The following programs can be considered example programs demonstrating the strings in C:

C Online Test


« Previous Tutorial Next Tutorial »