Menu
CoddyTech

헤더와 링크

Coddy의 C++ 개발자를 위한 C 코스 레슨. 48개 중 18번째.

C와 C++는 모두 헤더를 사용해 번역 단위를 컴파일합니다. C 헤더에는 C++ 템플릿, 네임스페이스, 오버로드 또는 클래스 선언이 들어갈 수 없습니다. C++ 사용자는 C 언어 연결을 요청하기 위해 일반적으로 C API를 extern "C"로 감쌉니다.

헤더는 번역 단위 간에 공유되는 인터페이스를 선언합니다. 헤더 가드는 선언이 반복되는 것을 방지하고, 정의는 소스 파일에 둡니다.

C++ 예제

#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <array>
#include <map>
#include <memory>
#include <algorithm>
#include <functional>
#include <variant>
#include <sstream>
std::string bracket(const std::string& s){return "["+s+"]";}
int main(){
std::cout<<bracket("pen")<<"\n";
}

C++ 출력:

[pen]

C 예제

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <stdbool.h>
#include <limits.h>
#include <errno.h>
static int total(int price,int count){return price*count;}
int main(void) {
printf("%d\n",total(3,4));
return 0;
}

C 출력:

12
challenge icon

챌린지

쉬움

lesson.h에 공개 함수로 선언된 subtotal(price,count)를 구현하세요. 필요하다면 lesson.c에서 비공개 static multiply 도우미 함수를 사용하세요. price와 count를 곱한 값을 반환하세요. 채점기는 도우미 함수의 작성 방식이 아니라 동작을 확인합니다.

잠금 해제된 소스 파일과 헤더 파일을 수정하세요. 해당 함수를 직접 호출하는 잠긴 main.c와 호환되도록 선언된 공개 API를 유지하세요. 다른 main을 추가하지 마세요. 작업에서 범위를 명시적으로 확인하지 않는 한, 모든 숫자 작업 입력값은 오버플로를 피할 만큼 작습니다. 지정된 출력만 출력하세요. 표시된 경우 드라이버가 입력 파싱과 출력을 제공합니다.

직접 해보기

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <stdbool.h>
#include <limits.h>
#include <errno.h>
#include "lesson.h"
int main(void){int a=0,b=0;if(scanf("%d%d",&a,&b)!=2)return 1;printf("%d\n",subtotal(a,b));return 0;}
quiz icon실력 점검

이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.

C++ 개발자를 위한 C의 모든 레슨

직접 연습해 보세요: 온라인 C 컴파일러