Computer >> 컴퓨터 >  >> 프로그래밍 >> C++

C 언어 표준 헤더 파일 완벽 정리: 종류와 활용 예제

C 언어에서 헤더 파일(header file)은 미리 정의된 표준 라이브러리 함수들의 집합을 담고 있는 파일입니다. 확장자가 .h인 헤더 파일을 프로그램에 포함하려면 전처리기 지시문인 #include를 사용합니다.

C 언어의 주요 표준 헤더 파일

아래 표는 C 언어에서 자주 사용되는 대표적인 헤더 파일과 그 역할을 정리한 것입니다.

번호헤더 파일 및 설명
1stdio.h
표준 입력/출력 함수 (printf, scanf 등)
2conio.h
콘솔 입출력 함수 (getch, putch 등)
3stdlib.h
범용 유틸리티 함수 (메모리 할당, 문자열 변환 등)
4math.h
수학 관련 함수 (pow, sqrt, sin 등)
5string.h
문자열 처리 함수 (strcpy, strlen 등)
6ctype.h
문자 처리 함수 (isalpha, toupper 등)
7time.h
날짜 및 시간 관련 함수
8float.h
부동소수점 타입의 한계값 정의
9limits.h
기본 데이터 타입의 크기 및 한계값 정의
10wctype.h
와이드 문자(wide character) 데이터의 타입을 판별하는 함수

헤더 파일 활용 예제

다음 예제는 여러 헤더 파일을 포함하여 각 헤더에 속한 함수들을 실제로 사용하는 방법을 보여줍니다.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

int main() {
    char s1[20] = "53875";
    char s2[10] = "Hello";
    char s3[10] = "World";
    int res;

    res = pow(8, 4);
    printf("Using math.h, The value is : %d\n", res);

    long int a = atol(s1);
    printf("Using stdlib.h, the string to long int : %d\n", a);
    
    strcpy(s2, s3);
    printf("Using string.h, the strings s2 and s3 : %s\t%s\n", s2, s3 );

    return 0;
}

실행 결과

위 프로그램을 실행하면 다음과 같은 출력 결과를 확인할 수 있습니다.

Using math.h, The value is : 4096
Using stdlib.h, the string to long int : 53875
Using string.h, the strings s2 and s3 : World World

예제 코드 설명

  • math.h: pow(8, 4) 함수로 8의 4제곱인 4096을 계산합니다.
  • stdlib.h: atol() 함수를 사용해 문자열 "53875"를 long형 정수로 변환합니다.
  • string.h: strcpy() 함수로 s3의 내용("World")을 s2에 복사하여 두 문자열이 모두 "World"가 됩니다.

이처럼 헤더 파일을 적절히 포함하면 표준 라이브러리가 제공하는 다양한 기능을 손쉽게 활용할 수 있으며, 코드의 재사용성과 생산성이 크게 향상됩니다.