(11:00) 코드를 실행했을 때 이상한 결과가 막 출력됩니다.
396
작성한 질문수 22
11.5강의 11:00부분입니다.

void custom_put(const char* str); //Only two lines
int custom_put2(const char* str); //Add \n, return # of characters
int main()
{
/*
Custom function
*/
custom_put("Just ");
custom_put("Do it!");
printf("%d\n", custom_put2("12345"));
return 0;
}
void custom_put(const char* str)
{
while (*str != '\n') //while(*str)
putchar(*str++);
}
int custom_put2(const char* str)
{
int count = 0;
while (*str)
{
putchar(*str++);
count++;
}
putchar('\n');
return count;
}
교수님께서 작성하셨던 코드 그대로 다 입력을 했습니다.
빌드를 실행하면 정상적으로 빌드가 되는데, 콘솔창에서 정말 이상한 값들만 자동으로 쫙 출력됩니다.
이게 어떤 원인때문에, 이렇게 발생한 문제인지 알고 싶습니다.
답변 1
0
안녕하세요,
첨부하신 코드에 오타가 보입니다.
void custom_put(const char* str)
{
while (*str != '\n')
putchar(*str++);
}
가 아닌
void custom_put(const char* str)
{
while (*str != '\0') //while(*str)
putchar(*str++);
}
입니다.
*str이 줄바꿈(\n)을 할 때까지 읽는 것이 아니라 문자열의 끝을 나타내는 \0까지 읽어들이는 것입니다.
줄바꿈할 때까지 putchar를 호출했기에 쓰레기값들이 출력된 것입니다.
Export template 안됨
1
65
2
완전히 똑같이 따라해도 exe파일이 안만들어져서 실행이 안됩니다.
1
96
3
main 함수에서 왜 int만 선언이 되는걸까요
1
84
2
8비트 2진수 변환시 왜 1을 더해야하나요?
1
81
2
혹시 강의를 빠르게 수강하려면 어디서부터 듣는게 좋을까요?
1
81
1
프로토타입과 함수간의 인자 불일치
1
87
2
12.12 헤더 관련 질문
1
74
2
Visual Studio Community 2026 사용 문의
1
171
2
Q. 15:30, 부호가 있는 8비트 정수 질문
1
73
2
getchar(), putchar()
1
112
3
강의자리ㅛ
1
93
2
비주얼스튜디오코드로 공부해도 상관없나요?
1
131
2
소스파일안에 여러 파일
1
88
2
F5와 F7의 차이
1
91
2
c = TWO * (a+b); 에서 a와 b는?
1
68
2
; 세미콜론을 붙이는 기준에 문의
1
78
1
Step over 기능 문의
1
66
2
2.6 강의 따옴표 출력 규칙 문의
1
87
2
int main 함수 관련 오류 문의
1
82
2
13.4 words[0]
0
73
2
11.7 함수를 구현해 봤습니다.
1
67
2
11.6 직접 strcmp와 strncmp를 구현해 보았습니다.
1
71
2
11.6 my_strcat과 my_strncat을 구현해봤습니다.
1
62
2
11.6 fit_str함수를 구현해 봤습니다.
1
60
2





