Skip to the content of the web site.

Strings in C

In C, a char is a 8-bit character allowing 256 values from 0 to 255. A sequence of n characters is represented in C as an array of n + 1 characters where the last character is the special character '\0\ (the null character which has the numeric value of 0.

Aside: Recall that letters such as 'A' through 'Z' and 'a' through 'z' are represented by numeric values between 65 and 90, and 97 and 122, respectively. For more information on ASCII characters, see the wikipedia page on them.

Because strings are arrays of char, they can be declared as either

       char *str1   = "Hello ";
       char  str2[] = "World!";

A string can be printed in printf as part of a string using the placeholder %s. For example,

#include <stdio.h>

int main() {
	char *str1   = "Hello ";
	char  str2[] = "World!";

	printf( "This is a string:  >>%s%s<<\n", str1, str2 );

	return 0;
}

The output is:

% gcc string.0.c 
% ./a.out 
This is a string:  >>Hello World!<<
%

If you want to allocate memory for a string, you must request at least one more character than the longest string you expect to store:

        char *str3 = malloc( 50*sizeof( char ) );

Technically, a char is one byte, and thus the sizeof( char ) is redundant; however, it is useful inform readers what your intent is.

Next, to enter data into that string from the user, it is possible to use the scanf function:

#include 
#include 

int main() {
        char *str3 = malloc( 50*sizeof( char ) );

        printf( "Enter a string: " );
        scanf( "%s", str3 );
        printf( "You entered: \"%s\"\n", str3 );

        return 0;
}

When this is run, you will note that scanf only extracts the characters up to the first white space:

% gcc string.1.c
% ./a.out 
Enter a string: Freude, schoener Gottesfunken
You entered: "Freude,"
%

For the most part, you will not have to do much more with strings in this course. Usually, working with strings ends up with the use of a number of functions that are included in the string.h library. For more information on these, you can look at the wikipedia page on string.h; however, what follows is an extraction from that page:

String Manipulation
strcpycopies one string to another
strncpywrites exactly n bytes, copying from source or adding nulls
strcatappends one string to another
strncatappends no more than n bytes from one string to another
strxfrmtransforms a string according to the current locale
String Examination
strlenreturns the length of the string
strcmpcompares two strings
strncmpcompares a specific number of bytes in two strings
strcollcompares two strings according to the current locale
strchrfinds the first occurrence of a byte in a string
strrchrfinds the last occurrence of a byte in a string
strspnfinds in a string the first occurrence of a byte not in a set
strcspnfinds in a string the last occurrence of a byte not in a set
strpbrkfinds in a string the first occurrence of a byte in a set
strstrfinds the first occurrence of a substring in a string
strtoksplits string into tokens
Miscellaneous
strerrorreturns a string containing a message derived from an error code
Memory Manipulation
memsetfills a buffer with a repeated byte
memcpycopies one buffer to another
memmovecopies one buffer to another, possibly overlapping, buffer
memcmpcompares two buffers
memchrfinds the first occurrence of a byte in a buffer