Strings in C is an array of characters. Using string we store characters of any number.

What is Strings in C?

The strings in C is defined as an array of characters. The string is nothing but one-dimensional array which stores the characters and terminated by NULL character. The String always ends with NULL. We can access a single character from a string by its index.

What is the difference between the character array and string?
Both are used to stores a number of character (word). The main difference is string terminated with special character ‘\0’ or NULL character.

String Declaration

Syntax to declare a Strings in C:
char varname[size];
In the above syntax, varname is a name given to the string and size is a number of characters which we want to store in a string.

Example String in c:
char name[10];
In the above example, the name is variable which stores the characters up to 10 characters and null at the end.

Note: – String always ends with special character null character ‘\0’.

String initialization

In C programming there are different ways to initialize string which are as follows:
1. Initializing string with its size
Syntax: char varname[size]=”string”;
In the above syntax, we can initialize directly by assigning a string in double-quotes.
Example: char name[20]=”defineloop”;

2. Initializing string without mentioning its size
Syntax: char varname[ ]=”string”;
In the above syntax, we do not mention the size of the array in this case compiler directly calculates the size of the string according to a string we assigned.
Example: char name[ ]=”defineloop”;
The compiler creates the array with size 11.

3. Initializing string
Syntax : char varname[size n]={‘char1′,’char1′, … char n’};
In the above syntax, we can initialize string by a single character in single quotes until the size of the string.
Example: char name[5]={‘d’, ’e’, ’f’, ’i’, ‘n’, ‘e’, ‘l’, ‘o’, ‘o’, ‘p’, ’\0′};

These are the ways you can initialize string in c.

Memory representation of the string

Strings in C
Strings in C

String input

In c programming, there is two way to take string input. Using scanf() function or gets() function.

1. Scanf function
Taking a string as input using scanf function reads the only word because space is considered as a separator.
Syntax: Scanf(“%s”,&stringname);

2. Gets function
Taking a string as input using gets function reads multiple words including space in string and enter is considered as a separator.
Syntax: gets(string name);

String output

1. Printf function
Using printf function we can print a string on the screen.
Syntax: printf(“%s”,stringname);

2. Puts function
Puts function prints string on-screen and cursor point to the new line.
Syntax: puts(stringname);

String program in c

// C program to demonstrate string in c


int main()
char s1[] ="Defineloop-----------";
char s2[20];
printf("\nEnter string = ");
printf("\nstring 2 = %s ", s2);

return 0;

Output: –String program in cString functions in c

String function in c provides a collection of inbuilt library functions. For using these String functions in c we first have to include the library header file #include<string.h>. Without including this header file and using any string functions will give you can compiler error.
There is the number of different string functions available in c programming which performs various string operations.

String functions in c:-

1. strlen()

strlen() function is used to calculate the length of the string. It takes a string as an argument to the function and returns integer length of the string.
Syntax for strlen() :-
int variable = strlen(stringname);
Example: –
char s[10] = “strlen”;
int len=strlen(s);
len => 6

2. strcat()

This function used to join two strings. It accepts two arguments first and second string. First string concatenated with Second string. The concatenated string stored in the first string.
Syntax for strcat() :-
strcat(first-string, second-string);
Example: –
char s1[10] = “string”;
char s2[30] = “concatenated”;
s1 => stringconcatenated

3. strcpy()

This function used to copy one string to another. It accepts two arguments first and second string. First string copied into Second string.
Syntax for strcpy :-
strcpy(destination_string, source_string);
Example: –
char s1[10] = “str”;
char s2[10];
s2 => str

4. strcmp()

This function used to compare two strings. It returns ASCII values if it matches it returns zero.
Syntax for strcmp(): –
strcmp(string1, string1);
strcmp returns 3 different values as follows:
Zero ( 0 ): A value equal to zero when both strings are found to be identical. That is, all of the characters in both strings are same.
Greater than zero ( >0 ): A value greater than zero is returned when the first not matching a character in the left string have the greater ASCII value than the corresponding character in right string.
Less than Zero ( <0 ): A value less than zero is returned when the first not matching the character in left String have lesser ASCII value than the corresponding character in right String.
Example: –
char s1[10] = “str”;
char s2[10] = “str”;
strcmp(s2,s1); => 0

5. strrev()

This function used to reverse the given string. It takes one string argument and returns string reversed.
Syntax for strrev(): –
Example: –
char s1[10] = “str”;
strrev(s1); => rts

C program for string functions in c


int main()
char name[16] ="Defineloop";

char name2[20]="Loop";

char name3[30],rev[20];

int length;

printf("String functions .........................");
printf ("\n1.Length of string :-----\n string length of %s =%d",name,length);

printf ("\n\n2.Copy string :----- ");


printf ("\n copied string =%s",name);

printf("\n\n3.Concatenate String :-----");


printf("\nconcatenated string=%s",name);

return 0;


String functions in c

Write A Comment