Arrays and Strings
String Length
A string scan usually stops when it reaches the null terminator.
String Length
string_length.c
#include <stdio.h>
int main(void) {
char text[5] = {'h', 'i', '\0', 'x', '\0'};
int stopAtNull = ;
int length = 0;
while (length < 5) {
if (stopAtNull && text[length] == '\0') {
break;
}
length++;
}
printf("length=%d\n", length);
return 0;
}
#include <stdio.h>
int main(void) {
char text[5] = {'h', 'i', '\0', 'x', '\0'};
int stopAtNull = ;
int length = 0;
while (length < 5) {
if (stopAtNull && text[length] == '\0') {
break;
}
length++;
}
printf("length=%d\n", length);
return 0;
}
scan
A scan moves one character at a time through an array.
terminator check
Stopping at `'\0'` prevents hidden buffer contents from being treated as text.