헤더와 링크
Coddy의 Python 개발자를 위한 C 코스 레슨. 48개 중 18번째.
Python의 import는 모듈 네임스페이스를 불러옵니다. C의 #include는 컴파일 전에 텍스트를 포함합니다. 선언과 공유 타입은 가드가 설정된 헤더에 넣고, 정의는 소스 파일에 넣은 뒤, 각 소스 파일을 한 번씩 컴파일하세요. 파일 범위의 static 도우미 함수는 해당 번역 단위에서만 사용할 수 있습니다.
Python
def total(price,count):
return price*count
print(total(3,4))C17
#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;
}
이식 규칙
가드가 설정된 헤더를 통해 함수 선언을 공유하고 소스 정의를 링크하세요.
챌린지
쉬움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;}
이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.
Python 개발자를 위한 C의 모든 레슨
직접 연습해 보세요: 온라인 C 컴파일러