'A'==65
'&'==38
'5'==53
'2'
is not the same as 2
. Really!
'\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';
┌────┬────┬────┬────┬────┬────┐ │ 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";
strcpy
: The C string library provides a function for copying strings:
char name[4]; strcpy(name, "sam"); // 's', 'a', 'm', '\0'
// These all do the same thing: char name1[5] = "Zulu"; char name2[] = "Zulu"; char name3[5] = { 'Z', 'u', 'l', 'u', '\0' }; char name4[] = { 'Z', 'u', 'l', 'u', '\0' }; ┌────┬────┬────┬────┬────┐ │ Z │ u │ l │ u │ \0 │ └────┴────┴────┴────┴────┘ 0 1 2 3 4 /* This one has extra space for growth */ char name5[10] = { 'Z', 'u', 'l', 'u', '\0' }; ┌────┬────┬────┬────┬────┬────┬────┬────┬────┬────┐ │ 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"; strcpy(state,"Texas"); printf("%zd\n", strlen(state)); printf("%zd\n", sizeof(state));
5 50
'\0'
(doesn’t include the '\0'
). It is returned by strlen
.
sizeof
.
%zd
is how you print a length.
char alpha[] = "123"; char beta[16] = "1234567890abcdef"; char gamma[] = "12345678"; printf("%zd %zd %zd\n", sizeof(alpha), sizeof(beta), sizeof(gamma)); printf("%zd %zd %zd\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
if (strcmp(string1, string2) == 0) printf("The strings are equal.\n");
strcmp
, not strequal
. The result of 0
for equal strings
can be confusing:
if (strcmp(string1, string2)) // Bad code! Bad! printf("They're the same.\n"); // Liar!
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?
'\0'
.
strcpy
to assign strings, not =
.
strcmp
to compare, not ==
, <
, >
, etc.
strcmp
results.
strcpy
and strcat
Modified: 2016-07-30T17:29 User: Guest Check: HTML CSSEdit History Source |
Apply to CSU |
Contact CSU |
Disclaimer |
Equal Opportunity Colorado State University, Fort Collins, CO 80523 USA © 2015 Colorado State University |
![]() |