char[8] str = "--------"를 출력하면 --------가 찍힐까?

jinwook han·2024년 2월 28일
0

char[8] str = "--------"를 출력하면 --------가 찍힐까?
-> printf("%s\n") 등으로 출력하면 쓰레기값이 출력될 수 있다.

코드

#include <stdio.h>
  
int main(int argc, char **argv) {
        char str[8] = "--------";
        printf("%s\n", str);
        return 0;
}

결과:

char[8] str = "--------"
배열 사이즈가 8이고, 배열에 null이 없었기 때문에 쓰레기값이 출력됐다.
char[] str = "--------"로 null값이 자동으로 들어가게 하거나, char[9] str = "--------\0"을 사용하면 쓰레기값 없이 출력할 수 있다.

참고자료:
https://blog.naver.com/gksdlsgn2006/130170329695

0개의 댓글

관련 채용 정보