'A'
≡ 65
'&'
≡ 38
'5'
≡ 53
'2'
is not the same as 2
. Really!
printf("%d %d %d %d\n", 'C', '%', '2', '\n');
67 37 50 10
'\0'
'\0'
is the null (not NULL
) character char state[6]; state[0] = 'T'; state[3] = 'a'; state[1] = 'e'; state[4] = 's'; state[2] = 'x'; state[5] = '\0'; printf("%s\n", state);
Texas
┌────┬────┬────┬────┬────┬────┐ │ T │ e │ x │ a │ s │ \0 │ └────┴────┴────┴────┴────┴────┘ 0 1 2 3 4 5
"hello"
.
'\0'
to end the string.
'\0'
.
char name[7]; name[0] = 'F'; name[1] = 'o'; name[2] = 'o'; printf("%s\n", name); ┌───┬───┬───┬───┬───┬───┬───┐ │ F │ o │ o │ ¥ │ ¶ │ € │ ☃ │ └───┴───┴───┴───┴───┴───┴───┘ 0 1 2 3 4 5 6
char sam1[4] = {'s','a','m','\0'}; char sam2[4] = "sam"; char sam3[] = "sam"; printf("%s %s %s\n", sam1, sam2, sam3);
sam sam sam
strcpy
: The C string library provides a function for copying strings: char name[4]; strcpy(name, "sam"); // 's', 'a', 'm', '\0' printf("My name is \"%s\"\n", name);
My name is "sam"
Multiple ways to define a character array as a string:
// These all do the same thing: char z1[5] = "Zulu"; char z2[] = "Zulu"; char z3[5] = { 'Z', 'u', 'l', 'u', '\0' }; char z4[] = { 'Z', 'u', 'l', 'u', '\0' }; /* This one has extra space for growth */ char z5[10] = { 'Z', 'u', 'l', 'u', '\0' }; printf("%s%s%s%s%s\n", z1, z2, z3, z4, z5);
ZuluZuluZuluZuluZulu
┌────┬────┬────┬────┬────┐ z1…z4: │ Z │ u │ l │ u │ \0 │ └────┴────┴────┴────┴────┘ 0 1 2 3 4 ┌────┬────┬────┬────┬────┬────┬────┬────┬────┬────┐ z5: │ Z │ u │ l │ u │ \0 │ ?? │ ?? │ ?? │ ?? │ ?? │ └────┴────┴────┴────┴────┴────┴────┴────┴────┴────┘ 0 1 2 3 4 5 6 7 8 9
#include <string.h>
strlen
— get the length of a string
strcpy
— copy one string into another
strcat
— add one string onto the end of another
strcmp
— compare two strings for equality
char state[50] = "Texas"; printf("%zu\n", strlen(state)); printf("%zu\n", sizeof(state));
5 50
'\0'
(doesn’t include the '\0'
). It is returned by strlen
.
sizeof
.
%zu
is how you print a length.
%ld
, %lu
, and %zd
are common, but wrong.
char alpha[] = "123"; char beta[16] = "1234567890abcdef"; char gamma[] = "12345678"; printf("%zu %zu %zu\n", sizeof(alpha), sizeof(beta), sizeof(gamma)); printf("%zu %zu %zu\n", strlen(alpha), strlen(beta), strlen(gamma));
4 16 9 3 19 8
strlen(beta)
possibly be more than sizeof(beta)
?
strcat
takes a character array as its first argument and
either a character array or string literal as its second argument.
char name[12]; strcpy(name, "Sue"); // name = "Sue", length=3 strcat(name, " Storm"); // name = "Sue Storm", length=9 strcat(name, " Richards"); // Runtime failure: too long strcat("Ben", "Grimm"); // Compile-time failure
char buf[10]; strcat(buf, "xyz"); // Adding on to the end of … what?
strcmp
returns 0
if two strings are equal and otherwise returns
a non-zero answer
char s1[] = "Russell Brand", s2[] = "Russell Brand"; if (strcmp(s1, s2) == 0) printf("The strings are equal.\n"); else printf("The strings are not equal.\n");
The strings are equal.
It’s strcmp
, not strequal
. The result of 0
for equal strings
can be confusing:
char s1[] = "Russell Brand", s2[] = "Russell Brand"; if (strcmp(s1, s2)) // Bad code! Bad! printf("They're the same.\n"); // Liar! else printf("The strings are not equal.\n");
The strings are not equal.
int strcmp(const char s1[], const char s2[]);
Return value indicates the lexicographical relation of string1 to string2:
Relationship of s1 to s2 | Value |
---|---|
s1 less than s2 | some number <0 |
s1 identical to s2 | 0 |
s1 greater than s2 | some number >0 |
Vague, isn’t it?
char a[] = "omega", b[] = "alpha"; printf("%d\n", strcmp(a,b));
14
'\0'
.
strcpy
to assign strings, not =
.
strcmp
to compare, not ==
, <
, >
, etc.
strcmp
results.
strcpy
and strcat