What are strings in C?

Strings are considered a group of characters. In the C language, a string is denoted with the help of a character array.

Basically, the end of the string is denoted by specifying a null \0 character inside the memory.

Syntax

A string can be declared by:

char a[10].

In the example above, variable a is a character array where you can store up to 10 characters.

Initialization

A string can be initialized by:

char a[5] = {'H', 'e', 'l', 'l' '\0'};
#include <stdio.h>
int main () {
char a[5] = {'H', 'e', 'l', 'l', '\0'};
printf("String: %s\n", a );
return 0;
}

String functions

Consider the two strings s1 and s2. Here are few built-in string functions that are available in the string.h header file:

  • strlen(s1): returns the length of a string.

  • strcpy(s1, s2): copies string s2 to s1

  • strrev(s1): reverses the given string

  • strcmp(s1, s2): returns 0 if s1 and s2 contain the same string. Similarly, it returns less than 0 if s1 < s2 and it returns greater than 0 if s1 > s2

  • strcat(s1, s2): concatenates two strings

Free Resources