소스파일과 헤더파일에 대해서

셤중의 이미지

다음과 같이 소스파일과 헤더파일을 만든다면 문제가 있나요?? 실행파일은 문제 없이 됩니다만...

ex)

// fct.h

int add(int n)
{
[공백] return ++n;
}

// fct.c 없음!

// main.c
#include [stdio.h] // < 표현이 안 되네요...
#include "fct.h"

int main(void)
{
[공백] int n = 5;

[공백] n = add(n);
[공백] printf("%d\n", n); // n=6 출력
[공백] return 0;
}

헤더파일에 변수를 선언 시 프로그램이 복잡해지면 문제가 발생할 수 있다는 건 이해가 됩니다.
그러나 헤더파일 내용은 단순히 선언과 구조체만이 그리고 소스파일에는 정의된 내용들이 있어야 한다는 것이 이해가 안 됩니다. 무슨 문제가 발생하여 이렇게 정해진 것인지.
단순히 시간이 흐르다 정해진 관례인 건가요??
제가 제시한 내용을 보면 굳이 정의 내용을 담은 소스파일(fct.c)과 그에 따른 헤더파일(fct.h)을 나눠야 하는 필요성을 못 느끼겠습니다. 질문을 정리하면.

1. 헤더파일의 내용에 함수 정의를 쓰면 안 되는 이유.
2. 헤더파일과 소스파일. 내용을 기준으로 만들어 구분하는 이유.

새해 복 많이 받으시구 감사합니다.

p.s. 소스파일 만들지 않고 헤더파일만 추가로 연관 있는 것끼리 구분해 만들면 된다... 가 저의 의견입니다.

DarkSide의 이미지

동일한 헤더 파일을 여러 소스 파일에서 인클루드하는 경우를 생각해보면 자명해지죠.

peecky의 이미지

헤더에 함수 정의가 있을 경우, include가 복잡해지다 보면, 함수 정의가 중복될 수 있기 때문입니다.

예를 들어,

// a.h:
int a() {
	return 0;
}
 
 
 
// b.h:
#include "a.h"
 
int b() {
	a();
	return 0;
}
 
 
 
// main.c:
#include "a.h"
#include "b.h"
 
int main() {
	a();
	b();
	return 0;
}

위와 같은 코드에서 main.c를 컴파일하기 전에 프리프로세서가 #include 부분을 처리하면 main.c는
int a() {
	return 0;
}
int a() {
	return 0;
}
 
int b() {
	a();
	return 0;
}
 
int main() {
	a();
	b();
	return 0;
}

가 되고, a() 함수가 중복으로 정의 되어서 컴파일이 되지 않습니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.