Scientific Programming in C V. Strings Susi Lehtola 1 November 2012
C strings As mentioned before, strings are handled as character arrays in C. String constants are handled as constant arrays. const char h e l l o []= Hello, world! ; const char h e l l o 2= Hello, world! ; The const qualifier (applicable to any datatype) states that the variable cannot be changed. const is the most misunderstood qualifier in C, since it can have different meanings. Scientific Programming in C, fall 2012 Susi Lehtola Strings 2/16
const example #i n c l u d e <s t d i o. h> i n t main ( void ) { / i n t e g e r / i n t i ; / i n t e g e r c o n s t a n t / const i n t c i =0; / p o i n t e r to i n t e g e r / i n t i p ; / p o i n t e r to c o n s t a n t i n t e g e r / const i n t c i p ; / c o n s t a n t p o i n t e r to i n t e g e r / i n t const i c p=&i ; / c o n s t a n t p o i n t e r to i n t e g e r c o n s t a n t / const i n t const c i c p=&c i ; } / Set p o i n t e r to i n t e g e r / i p=&i ; / Set p o i n t e r to c o n s t a n t i n t e g e r / c i p=&c i ; r e t u r n 0 ; Scientific Programming in C, fall 2012 Susi Lehtola Strings 3/16
const example, cont d #i n c l u d e <s t d i o. h> i n t main ( void ) { / i n t e g e r / i n t i ; / i n t e g e r c o n s t a n t / const i n t c i =0; / p o i n t e r to i n t e g e r / i n t i p ; / p o i n t e r to c o n s t a n t i n t e g e r / const i n t c i p ; } i p=&i ; / This i s a l s o a l l o w e d / c i p=i p ; / But to go i n the o t h e r way you need a c a s t. I t s g e n e r a l l y not a good i d e a to change c o n s t a n t s. / i p =( i n t ) c i p ; r e t u r n 0 ; Scientific Programming in C, fall 2012 Susi Lehtola Strings 4/16
static Another commonly used qualifier is static. When a global variable (defined outside of functions) or a function is defined as static, it is only visible to code inside the same source code file. External static can thus be used to hide implementation details. When an internal static is used for a variable (i.e., inside a function), the variable becomes permanently available inside the function: it is not created and destroyed every time the function is executed (analogous to the SAVE qualifier in Fortran). Scientific Programming in C, fall 2012 Susi Lehtola Strings 5/16
Strings, cont d Since in general there is no way to know the length of an array in C, strings are terminated with a special null character \0 in C. H e l l o w o r l d! \0 Because of the very low level approach to strings in C, people usually prefer to perform string manipulation in a higher level language. Scientific Programming in C, fall 2012 Susi Lehtola Strings 6/16
Special characters C has the following special characters: alert (bell) \a backspace \b form-feed \f carriage return \r horizontal tab \t vertical tab \v newline \n backslash \\ questionmark \? single quote \ double quote \ octal number \ooo octal number \xhh Scientific Programming in C, fall 2012 Susi Lehtola Strings 7/16
Character functions There are helper functions to determine what kind of a character is in question. #i n c l u d e <c t y p e. h> i n t i s a l n u m ( i n t c ) ; i n t i s a l p h a ( i n t c ) ; i n t i s c n t r l ( i n t c ) ; i n t i s d i g i t ( i n t c ) ; i n t i s g r a p h ( i n t c ) ; i n t i s l o w e r ( i n t c ) ; i n t i s p r i n t ( i n t c ) ; i n t i s p u n c t ( i n t c ) ; i n t i s s p a c e ( i n t c ) ; i n t i s u p p e r ( i n t c ) ; i n t i s x d i g i t ( i n t c ) ; Scientific Programming in C, fall 2012 Susi Lehtola Strings 8/16
Character functions, cont d isalnum() checks for an alphanumeric character, returns (isalpha(c) isdigit(c)). isalpha() checks for an alphabetic character. iscntrl() checks for a control character. isdigit() checks for a digit (0 through 9). isgraph() checks for any printable character except space. islower() checks for a lower-case character. isprint() checks for any printable character including space. ispunct() checks for any printable character which is not a space or an alphanumeric character. isspace() checks for white-space characters. isupper() checks for an uppercase letter. isxdigit() checks for a hexadecimal digits, that is, one of 0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F. Scientific Programming in C, fall 2012 Susi Lehtola Strings 9/16
String functions There is also a bunch of string-related functions in C, available in the string.h header file. v o i d memcpy ( v o i d r e s t r i c t, c o n s t v o i d r e s t r i c t, s i z e t ) ; v o i d memset ( v o i d, i n t, s i z e t ) ; char s t r c a t ( char r e s t r i c t, c o n s t char r e s t r i c t ) ; i n t strcmp ( c o n s t char, c o n s t char ) ; char s t r c p y ( char r e s t r i c t, c o n s t char r e s t r i c t ) ; s i z e t s t r l e n ( c o n s t char ) ; Scientific Programming in C, fall 2012 Susi Lehtola Strings 10/16
memcpy #i n c l u de <s t r i n g. h> void memcpy ( void dest, const void s r c, s i z e t n ) ; The memcpy() function copies n bytes from memory area src to memory area dest. The memory areas must not overlap. Use memmove(3) if the memory areas do overlap. Scientific Programming in C, fall 2012 Susi Lehtola Strings 11/16
memset #i n c l u de <s t r i n g. h> void memset ( void, int, s i z e t ) ; The memset() function fills the first n bytes of the memory area pointed to by s with the constant byte c. Scientific Programming in C, fall 2012 Susi Lehtola Strings 12/16
strcat #i n c l u de <s t r i n g. h> char s t r c a t ( char r e s t r i c t, const char r e s t r i c t ) ; The strcat() function appends the src string to the dest string, overwriting the terminating null byte ( \0 ) at the end of dest, and then adds a terminating null byte. The strings may not overlap, and the dest string must have enough space for the result. The strncat() function is similar, except that it will use at most n characters from src; and src does not need to be null-terminated if it contains n or more characters. Scientific Programming in C, fall 2012 Susi Lehtola Strings 13/16
strcmp #i n c l u de <s t r i n g. h> i n t strcmp ( const char s1, const char s2 ) ; i n t strncmp ( const char s1, const char s2, s i z e t n ) ; The strcmp() function compares the two strings s1 and s2. It returns an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2. The strncmp() function is similar, except it only compares the first (at most) n characters of s1 and s2. Scientific Programming in C, fall 2012 Susi Lehtola Strings 14/16
strcpy #i n c l u de <s t r i n g. h> char s t r c p y ( char dest, const char s r c ) ; char s t r n c p y ( char dest, const char s r c, s i z e t n ) ; The strcpy() function copies the string pointed to by src, including the terminating null byte ( \0 ), to the buffer pointed to by dest. The strings may not overlap, and the destination string dest must be large enough to receive the copy. The strncpy() function is similar, except that at most n bytes of src are copied. Warning: If there is no null byte among the first n bytes of src, the string placed in dest will not be null-terminated. Scientific Programming in C, fall 2012 Susi Lehtola Strings 15/16
strlen #i n c l u de <s t r i n g. h> s i z e t s t r l e n ( const char s ) ; The strlen() function calculates the length of the string s, excluding the terminating null byte ( \0 ). Scientific Programming in C, fall 2012 Susi Lehtola Strings 16/16