반응형
char null 터미네이터가 길이 카운트에 포함되어 있습니다.
#include <stdio.h>
int main(int argc, char *argv[]) {
char s[]="help";
printf("%d",strlen(s));
}
위의 출력이 4인 이유는 5가 정답 아닌가요?
메모리에는 'h', 'e', 'l', 'p', '0'이어야 합니다.
고마워요.
strlen
: null 터미네이터를 포함하지 않는 지정된 바이트 문자열의 길이를 반환합니다.
char s[]="help";
strlen(s) should return 4.
sizeof
: 지정된 바이트 문자열의 길이를 반환합니다(null 터미네이터 포함).
char s[]="help";
sizeof(s) should return 5.
strlen
는 늘 문자에 도달할 때까지 요소를 카운트하며, 이 경우 카운트를 중지합니다.길이에는 포함되지 않습니다.
strlen()
에서는 어레이 내의 문자수는 카운트되지 않습니다(실제로 이것은 알 수 없는 경우가 있습니다).배열이 아닌 메모리에 대한 포인터만 있는 경우).이미 알게 된 바와 같이 Null 문자를 포함하지 않고 최대 문자 수를 계산합니다.고려하다char s[] = {'h','i','\0','t','h','e','r','e'};
4입니다.
strlen()은 값이 0인 첫 번째 문자(nul 터미네이터)까지를 카운트합니다(포함하지 않습니다).
strlen(const char* ptr)
는 0이 아닌 요소를 0에 도달할 때까지 카운트하여 문자열 길이를 반환합니다.그렇게'\0'
안 돼.
이러한 질문은 링크와 같은 참조를 참조할 것을 권장합니다.
다음과 같이 명시되어 있습니다.
A C string is as long as the number of characters between the beginning
of the string and the terminating null character (without including the
terminating null character itself).
언급URL : https://stackoverflow.com/questions/14905937/is-that-char-null-terminator-is-including-in-the-length-count
반응형
'source' 카테고리의 다른 글
Vue-Devtools 쉘을 사용한 원격 디버깅 - net:ERR_CONNECTION_REFUSED (0) | 2022.08.25 |
---|---|
Vuex: Store에서 컴포넌트 데이터 또는 호출 컴포넌트 메서드를 업데이트하는 방법 (0) | 2022.08.24 |
vue-tables-2 서버 측에 범위 필터를 추가하는 방법 (0) | 2022.08.24 |
Init pinia 상태 (0) | 2022.08.24 |
C의 printf 형식 문자열에 %c와 %s가 모두 있는 이유는 무엇입니까? (0) | 2022.08.24 |