Strings in C Programming

Last Updated Nov 20, 2015, 07:00:14 PM





Strings in C programming can be represented as an of characters. You can think of a string as your name. However, in C strings always end with a string null character.

The null character is represented by another escape sequence, \0. C programming, has built-in string handling which allows us to use the string constants these string constants are also called as literals

If you create strings using double quotes, C will automatically create an array of characters for us which will also contain a terminator escape sequence \0. For example, let us see a sample string declaration.

Syntax

The above example represents an array of string characters placed in between double quotes. Always note that, we do not need to place the null character at the end, the C compiler will automatically do that for us.

Example

Try It Now

Declaring Strings

Strings in C language can be declared with an array of characters if you already know the length of the string you give the size otherwise you can leave it empty.

Initializing String

As we already know what initialization is, we assign a value to the declared string or variable. There are different ways we can initialize strings in c programming. Let us see them.

Method 1 Try It Now

Method 2 Try It Now

Method 3 Try It Now

Method 4 Try It Now

C String strcpy Method

C strcpy method is used to copy one string to another string

Try It Now

strcat(s1, s2)

C string strcat method is used to concatenate one string with another

Try It Now

strlen(s1)

This method is used to return the length of a string

Try It Now

strcmp

This method is used to compare two strings and return the the output



strchr

This method is used to return a pointer in the first occurance



Other string examples you might want to learn

Concatenate two strings in c

Remove characters in string except alphabets in c

Find number of vowels consonants and white spaces string in c

Find length of a string in c

Copy string without using strcpy in c



How do I create an array of strings in C? - StackOverFlow

What is the difference between char s[] and char *s in C? - StackOverFlow