Chapter 9 강의에서 질문입니다.

hjhj5078 Reply 3 years 50 weeks ago
Main.c 에 hotels.h 가 있을 경우에 hotels.h 와 hotels.c 를 모두 사용할 수 있게 강의에서 설명해주셨는데 이건 #include "hotels.h" 를 하면 hotels.c를 사용할 수 있게 된다는 것인가요??
hjhj5078 Reply 3 years 50 weeks ago
질문이 애매한거 같아서 다시 질문드립니다 ㅎㅎ.. Main.c 안에 #include "hotels.h" 가 들어있을 경우에 강의 안 설명에서는 hotels.h 와 hotels.c 를 모두 사용할 수 있었는데 이는 #include "hotels.h" 라는 문장을 썻을시 hotels.c라는 파일도 사용할수 있게 되기 때문인가요??
withcs2 Reply 3 years 50 weeks ago
hotels.h와 hotels.c는 전혀 다른 파일입니다. #include "hotels.h" 없이도 hotels.c는 실행됩니다. 실행하면 main()이 실행되기 때문에 main()이 들어있는 Main.c만 실행된다고 생각할 수도 있는데 실제로는 Main.c와 hotels.c 둘 다 실행되는 중이었습니다. 아래 프로젝트들을 만들어서 실행해보면 이해하는데 도움이 될 것 같습니다. 프로젝트1 * Main.c #include <stdio.h> #include "header.h" int main(){print_nothing();} * header.h void print_nothing(); * nothing.c void print_nothing(){printf("nothing\n");} 프로젝트2 * nothing1.c #include <stdio.h> void print_nothing(); int main(){print_nothing();} * nothing2.c void print_nothing(){printf("nothing\n");} 프로젝트3 * Main.c #include <stdio.h> void print_nothing(); int main(){print_nothing();} void print_nothing(){printf("nothing\n");} 세 프로젝트의 결과는 전부 같습니다. 파일을 여러개로 분리하면 나중에 재사용하기 편하다는 장점이 있습니다.