Strings in C Programming Language

A string in C is a series of characters in a group that occupy contiguous memory. A group of characters (Alphabets, digits and special characters) is called as a string.

Example 1:

A string in C language should always be enclosed with in double quotes (“).
If single quote is used then it is treated as character. Hence ‘A’ is different from “A”. Every string ends with a null character (‘\0’). Note that \0 is a single character. So when it has to be explicitly assigned to a string, it has to be written with in single quotes as ‘\0’.
A null character occupies 1 byte of memory.

Syntax:

Example 2:

The above statement declares an array named “name” capable of holding 20 characters. The 20th character is null character. Hence we can store 19 characters. The 20th character is reserved for null character.

Example 3:

Here,
‘L’ is stored at name[0]
‘e’ is stored at name[1]
‘a’ is stored at name[2]
‘r’ is stored at name[3]
.
.
.
.
‘e’ is stored at name[11]
And finally,
‘\0’ is stored at name[12].
name[14] and name[15] will contain garbage value.

Example 4:

Here the size of an array is calculated automatically (15 characters).

We can also declare string as character pointer as follows:

Printing Strings:

Using Character pointer:

Using Character Array:

printf function can be used to print a string in C. Similarly, puts function can be used to print a string as shown below.

Accepting String as input in C language:
We can use scanf or gets to accepts string from the user.

Using scanf in C:

The above statement passes the base address implicitly.

In the above statement, we are passing the address of the first element.

Using gets in C:

Although scanf and gets seems to be the same, there is a big difference between them. Let’s understand this difference.

Program using scanf in C language:

Output:

Program using gets in C language:

Output:

When a string in C is accepted using scanf function, if a space is encountered then it will treat it as null character(‘\0’). In the above example, a space encountered after “Learn” is treated as null character. Hence while printing, only “Learn” is printed.
This problem can be resolved using gets function.

You may also like...

18 Responses

  1. Aankhi says:

    In example 3, if the length of the string is 15, shouldn’t the index of the last position of the string be 14, since it starts with 0?

  2. LearnCOnline says:

    In the example 3 we have defined string name as
    char name[15]=

  3. Anonymous says:

    very nice teaching

  4. Rachana says:

    In example 4

    Using Character pointer:
    char *name =

  5. Anonymous says:

    #include
    int main()
    {
    char *str;
    gets(str);
    printf(“\n%s”,str);
    return 0;
    }
    when i run the above code i had seen a another dialog box close the program. why?

  6. Manoj Rathod says:

    @Rachana :-
    It will print the actual string . . .

  7. Anonymous says:

    When I do this:
    char *str;
    printf(

  8. Anonymous says:

    could you also use the following because I learned it is unsafe to use gets because it doesn’t have a buffer, and scanf doesn’t return the whole string.

    #include
    #include

    #define MAX_NAME_SZ 256
    int main(){
    //allocate some memory
    char *str = malloc(MAX_NAME_SZ);

    //check if it is OK
    if(*str == NULL){
    printf(“Sorry, there is not enough memory \n”);
    return 1;
    }

    //Use fgets to get the string
    fgets(str, MAX_NAME_SZ, stdin);

    //Remove trailing newline characters
    if((strlen(str) !=0) && (str[strlen(str)-1] == ‘\n’)){
    str[strlen(str)-1] = ‘\0’;
    }

    printf(” You Entered: %s”, str);
    return 0;
    }

  9. in example 4 printing strings using character array
    what is the use of three printf statements?

  10. Anonymous says:

    good

  11. sanjay says:

    char *str;
    what the above statement mean???

  12. kraze says:

    @sanjay

    char *str;

    here str is character type pointer which can store the address of some character variable. i.e. it points to a character.

  13. kraze says:

    @sanjay
    its a pointer which can store adress of some other character variable.

  14. shivam says:

    can c online teach me from basics ?? i have chosen computer engineering and am in 1st year nd not able to copeup everyone else in class so plz do advice something to fix me

    • Prashant N. says:

      Learn C Online can help you learn C programming language right from the scratch. All you need to do is go through each and every topics of c language and you should be able to excel C programming easily. Many readers have gained benefits from this website and you can be the another one.

      All the best and if you have any query then definitely post your comments and we shall try to resolve your queries as quick as possible.

      Thanks,
      Learn C Online Team

  15. Vinay says:

    Thank you

Leave a Reply

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