일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- 개발
- 오답풀이
- cording
- 프로그래밍 언어론
- git
- knockon
- 정보처리기능사
- Developer
- 홈서버
- 매크로
- html
- React
- plan
- study
- CSS
- WPF
- bootcamp
- 방화벽
- Mac
- 자격증
- 외부접속
- windows
- php
- 220821
- 프로그래밍언어론
- 개발공부
- 개인서버
- C
- Java
- CodeIgniter
- Today
- Total
bunta의 보조기억장치
[1주차 TIL] KnockOn Bootcamp 헤더파일 본문
💡 헤더 파일이란?
C와 C++에서 사용하는 일종의 참조 파일로 함수, 변수, 구조체, 매크로 등의 선언을 모아 놓은 파일이다.
보통 소스 코드의 맨 앞부분에 #include로 포함시키며, 컴파일 시 자동으로 연결되어 컴파일러가 사용할 수 있게 해준다.
크게 컴파일러가 제공하는 표준 헤더와 사용자가 임의로 만든 사용자 정의 헤더가 있다.
헤더 파일 사용법
#include <stdio.h>
#include "hacker.h"
// 표준 헤더의 경우 <>로 감싸야 함
// 사용자 헤더의 경우 ""로 감싸야 함
int main()
{
printf("Hello, World!\n");
return 0;
}
🎯사용자 헤더 만들기
1. .h 확장자를 가진 헤더 파일을 작성한 뒤, 그 안에 구현체나 함수, 변수 등을 선언한다.
2. 헤더 파일을 사용하려는 소스 파일의 맨 앞부분에 #include를 통해 포함시킨다.
예시)
student.h
#pragma once
#include <stdio.h>
typedef struct Student
{
char name[50];
} student;
int input_name(student*);
student* new_student()
{
student* new_ptr = (student*) malloc(sizeof(student));
memset(new_ptr->name, '\0', sizeof(new_ptr->name));
input_name(new_ptr);
return new_ptr;
}
int input_name(student* ptr){
write(1, "Input the name : ", 17);
return scanf("%s",ptr->name);
}
void say_hello(student* ptr)
{
printf("Hello, My name is %s.\n", ptr->name);
}
main.c
#include "student.h"
int main()
{
student* student = new_student();
input_name(student);
say_hello(student);
return 0;
}
출력 결과
Input the name : bunta
Hello, My name is bunta.
❗ 주의할 점
사용자 헤더파일의 경우에는 큰따옴표(" ")를 사용해서 작성해야 하는데 이는 컴파일러가 컴파일 시 < >로 감싸진 파일과 " "로 감싸진 파일을 찾아보는 경로가 다르기 때문이다.
형태 | 찾는 순서 | 용도 |
#include <파일명> | 시스템 경로만 탐색 | 표준 라이브러리 헤더 |
#include "파일명" | 현재 디렉토리 → 시스템 경로 | 사용자 정의 헤더 |
🔎 헤더 파일 중복 방지
헤더 파일을 중복으로 불러오게 되면 컴파일 에러가 발생할 수 있다.
예를 들어 위에서 작성한 student.h 헤더 파일을 포함한 class_room.h 라는 헤더 파일이 있다고 가정할 때 class_room.h를 main.c에 그대로 포함시킬 경우 main.c에서는 student.h가 중복으로 포함된 것과 같은 상태가 된다.
class_room.h
#include "student.h"
int class_room() {
...
}
student.h
#include <stdio.h>
typedef struct Student
{
...
} student;
main.c
#include "class_room.h"
// 헤더 중복 선언
#include "student.h"
int main()
{
...
}
위와 같은 상황을 방지하기 위해서 2가지 방법을 사용할 수 있다.
1. #ifndef 사용
#ifndef는 전처리기 코드이며 if not define 의 줄임말이다.
즉, 정의(define)되어 있지 않으면 #endif 가 나올 때까지 아래의 코드를 수행하고 정의되어 있다면 코드를 수행하지 않겠다는 의미이다.
예시)
student.h
#ifndef STUDENT_H
#define STUDENT_H
#include <stdio.h>
typedef struct Student
{
...
} student;
#endif
main.c
#include "class_room.h"
// student.h가 중복 추가되지 않음
#include "student.h"
int main()
{
...
}
class_room.h에서 student.h가 실행되어 STUDENT_H가 선언되므로 main.c에서 #include "student.h"가 적용되지 않는다는 것을 알 수 있다.
✅ 장점
기본적인 전처리기 코드이므로 컴파일러의 버전에 상관없이 컴파일이 된다.
❌ 단점
중복으로 include 되어있을 경우 해당 헤더 파일에 진입하여 #ifndef 문을 체크하기 때문에 컴파일 속도가 느릴 수 있다.
2. #pragma once
#pragma once는 컴파일러 전용 전처리기 지시문으로 해당 헤더파일이 포함되어 있을 경우 중복으로 포함되지 않도하는 역할을 한다.
예시)
student.h
#pragma once
#include <stdio.h>
typedef struct Student
{
...
} student;
✅ 장점
#ifndef 보다 사용이 간편하다.
❌ 단점
일부 오래된 컴파일러에서는 지원하지 않을 수도 있다.
'KnockOn Bootcamp' 카테고리의 다른 글
[2주차 TIL] KnockOn Bootcamp 탐색 알고리즘 (0) | 2025.04.15 |
---|---|
[2주차 TIL] KnockOn Bootcamp 정렬 알고리즘 (0) | 2025.04.15 |
[1주차 TIL] KnockOn Bootcamp 트리 (0) | 2025.04.08 |
[1주차 TIL] KnockOn Bootcamp 스택&큐 (0) | 2025.04.08 |
[1주차 TIL] KnockOn Bootcamp 연결 리스트 (0) | 2025.04.07 |