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 | |
strcpy | copies one string to another |
strncpy | writes exactly n bytes, copying from source or adding nulls |
strcat | appends one string to another |
strncat | appends no more than n bytes from one string to another |
strxfrm | transforms a string according to the current locale |
String Examination | |
strlen | returns the length of the string |
strcmp | compares two strings |
strncmp | compares a specific number of bytes in two strings |
strcoll | compares two strings according to the current locale |
strchr | finds the first occurrence of a byte in a string |
strrchr | finds the last occurrence of a byte in a string |
strspn | finds in a string the first occurrence of a byte not in a set |
strcspn | finds in a string the last occurrence of a byte not in a set |
strpbrk | finds in a string the first occurrence of a byte in a set |
strstr | finds the first occurrence of a substring in a string |
strtok | splits string into tokens |
Miscellaneous | |
strerror | returns a string containing a message derived from an error code |
Memory Manipulation | |
memset | fills a buffer with a repeated byte |
memcpy | copies one buffer to another |
memmove | copies one buffer to another, possibly overlapping, buffer |
memcmp | compares two buffers |
memchr | finds the first occurrence of a byte in a buffer |