C Tutorial – strings and string Library Functions

Important: Before you start this tutorial, did you follow the pointers and more on pointers tutorials? Strings and pointers are intertwined to a large extent.

A string in the C language is simply an array of characters. Strings must have a NULL or \0 character after the last character to show where the string ends. A string can be declared as a character array or with a string pointer. First we take a look at a character array example:


     char mystr[20];

As you can see the character array is declared in the same way as a normal array. This array can hold only 19 characters, because we must leave room for the NULL character.
Take a look at this example:


	#include<stdio.h>

	int main()
	{
		char mystring[20];

		mystring[0] = 'H';
		mystring[1] = 'E';
		mystring[2] = 'L';
		mystring[3] = 'L';
		mystring[4] = 'O';
		mystring[5] = '\n';
		mystring[6] = '\0';

		printf("%s", mystring);

		return 0;
	}

Note: %s is used to print a string. (The 0 without the ” will in most cases also work).

String pointers are declared as a pointer to a char. When there is a value assigned to the string pointer the NULL is put at the end automatically. Take a look at this example:


	#include<stdio.h>

	int main()
	{
		char *ptr_mystring;

		ptr_mystring = "HELLO";
		printf("%s\n", ptr_mystring);

		return 0;
	}

It is not possible to read, with scanf, a string with a string pointer. You have to use a character array and a pointer. See this example:


	#include<stdio.h>

	int main()
	{
        	char my_array[10];
        	char *ptr_section2;

        	printf("Type hello and enter\n");
       	        scanf("%s", my_array);
        	ptr_section2 = my_array;
        	printf("%s\n", ptr_section2);

        	return 0;
	}

string.h or strings.h

The C language provides no explicit support for strings in the language itself. The string-handling functions are implemented in libraries. String I/O operations are implemented in <stdio.h> (puts , gets, etc). A set of simple string manipulation functions are implemented in <string.h>, or on some systems in <strings.h>.

The string library (string.h or strings.h) has some useful functions for working with strings, like strcpy, strcat, strcmp, strlen, strcoll, etc. We will take a look at some of these string operations.

Important: Don’t forget to include the library string.h (or on some systems strings.h) if you want to use one of these library functions.

strcpy

This library function is used to copy a string and can be used like this: strcpy(destination, source). (It is not possible in C to do this: string1 = string2). Take a look at the following example:


	str_one = "abc";
	str_two = "def";
	strcpy(str_one , str_two); // str_one becomes "def"

Note: strcpy() will not perform any boundary checking, and thus there is a risk of overrunning the strings.

strcmp

This library function is used to compare two strings and can be used like this: strcmp(str1, str2).

  • If the first string is greater than the second string a number greater than null is returned.
  • If the first string is less than the second string a number less than null is returned.
  • If the first and the second string are equal a null is returned.

Take look at an example:



	printf("Enter you name: ");
	scanf("%s", name);
	if( strcmp( name, "jane" ) == 0 )
		printf("Hello, jane!\n");

Note: strcmp() will not perform any boundary checking, and thus there is a risk of overrunning the strings.

strcat

This library function concatenates a string onto the end of the other string. The result is returned. Take a look at the example:


	printf("Enter you age: ");
	scanf("%s", age);
	result = strcat( age, " years old." ) == 0 )
	printf("You are %s\n", result);

Note: strcat() will not perform any boundary checking, and thus there is a risk of overrunning the strings.

strlen

This library function returns the length of a string. (All characters before the null termination.) Take a look at the example:


	name = "jane";
	result = strlen(name); //Will return size of four.

memcmp

This library function compares the first count characters of buffer1 and buffer2. The function is used like this: memcmp(buffer1,buffer2). The return values are as follows:

  • If buffer1 is greater than buffer2 a number greater than null is returned.
  • If buffer1 is less than buffer2 a number less than null is returned.
  • If buffer1 and buffer2 are equal a null is returned.

Note: There are also library functions: memcpy, memset and memchr.

That is all for this tutorial.

This entry was posted in C Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed. Tweet This! Tweet This! or use to share this post with others.

There are currently 12 responses to “C Tutorial – strings and string Library Functions”

Why not let us know what you think by adding your own comment!

  1. mausumi das on February 11th, 2012:

    thanx a lot ……….its easy to learn from c tutorial

  2. prakash on April 25th, 2012:

    so many thanx. it is easy to learn me.

  3. pavel on September 6th, 2012:

    There is a typo in the “strcat” section: you have obviously copied the “== 0 )” from the previous example. I also noticed that it is very important to let the compiler know how long your string is going to be, even if it is a pointer-string (for “strcat”). If the string is longer than expected program crashes every time. P.s thanks for the C-language tutorial, it is very helpful

  4. vignesh.p on October 26th, 2012:

    tanks for guidance in basic c

  5. marcpaul on November 1st, 2012:

    thanks alot

  6. DeeDoyle on December 14th, 2012:

    Wondering why I can use strcmp in C without including the #include

  7. Mukesh on February 1st, 2013:

    very simple….as for all the functions you require to declare a prototype of the function to be used in the program, same way the prototype of strcmp() and all other library function is declared in strings.h file. So you need to compulsorily include the #include header file.

  8. rajkumar on April 7th, 2013:

    nice tutorial

  9. princess Angella on April 22nd, 2013:

    thanx,understandable and simple

  10. Hugo on August 1st, 2013:

    char *ptr_mystring;
    ptr_mystring = “HELLO”;

    >> I don’t understand… prt_mystring is supposed to be an ADDRESS pointing to a char.

    I can’t do:

    int *ptr_myint;
    prt_myint=103;

    so why are you able to put a string into a pointer ??

  11. MrSun on August 27th, 2013:

    @Hugo

    becase the int 103 is a value ,but “Hello” is a char array.Array is a special value,you can output a array to see printf(“%d\n”,arr) and its address value printf(“%u\n”,arr),and so。。。 what you see??(*^__^*) 嘻嘻

  12. MrSun on August 27th, 2013:

    int main(int argc, char** argv) {

    char *str1;
    char *str2;
    str1 = “sssssss”;
    str2 = “lllllll”;

    char *cat;
    //strcpy(&str1,&str2);
    strcat(str1,str2);

    printf(“str1 : %s \n”,str1);
    printf(“str2 : %s \n”,str2);
    printf(“str2 : %d \n”,strlen(str1));

    return 0;

    }

    It’s throw exceptions 0\xxx can’t be read ,I don’t know why,can someone help me?please。。。