Language/C
[따배씨] 16.6 #include 와 헤더파일
migrationArc
2021. 8. 22. 22:38
따배씨 - 따라하며 배우는 C언어
16강 전처리기와 라이브러리
16.6 #include 와 헤더파일
// my_functions.h
#pragma once
extern int status;
static int si = 0;
extern int add(int a, int b);
static int multiply(int a, int b)
{
return a * b;
}
//int substract(int a, int b)
inline int subtract(int a, int b)
{
return a - b;
}
void print_status(void);
void print_address(void);
#pragma once
- hader file 이 한번만 include 되도록 설정
#ifndef __MY_FUNCTIONS__
#define __MY_FUNCTIONS__
// header code
#endif
- hader file 이 한번만 include 되도록 설정 2
extern int a = 1;
// 전역번수 선언 및 초기화
- extern - 모든 파일에서 사용 가능한 전역 선언
- 다른 file 에서도 변수 사용 가능
- file scope 에서 전역변수 선언도 extern 을 default 로 한다
extern int a;
- 다른 file 에서 선언된 전역변수에 대한 '참조선언'
- extern 참조 선언은 함수 내에서도 가능한데, 이 경우에는 Compiler 가 다른 파일에서 extern 변수 선언을 검사하지 않음으로 Compile Error 가 발생하지 않음으로 주의
- extern 함수의 경우, 모든 함수는 기본적으로 extern 으로 선언됨
static int b = 2;
- static - 한 파일 내에서만 사용가능한 전역 선언
// my_functions.c
#include "my_functions.h"
#include <stdio.h>
int status = 0;
int add(int a, int b)
{
return a + b;
}
void print_status()
{
printf("my_functions header status address, value: %p, %d\n", &status, status);
}
void print_address()
{
printf("print_address()\n");
printf("Static function address %p\n", multiply);
printf("Static valiable address %p\n", &si);
}
// my_structures.h
#pragma once
typedef struct
{
char name[100];
int age;
} patient_info;
// my_headers/my_macros.h
#pragma once
#define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
#define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
#define ABS(X) ((X) < 0 ? -(X) : (X))
#define GRAVITY 9.8
#define PI 3.141592
// main.c
#include <stdio.h> // 공식직원, 표준 라이브러리
#include "my_functions.h"
#include "my_structures.h"
#include "my_headers/my_macros.h" //path
extern int status;
int main()
{
printf("PI = %f\n", PI);
// PI = 3.141592
printf("main status address, value: %p, %d\n", &status, status);
// main status address, value: 0x100008018, 0
print_status();
// my_functions header status address, value: 0x100008018, 0
printf("%d\n", multiply(51, 2));
// 102
printf("\nmain()\n");
// main()
printf("Static function address %p\n", multiply);
// Static function address 0x100003de0
printf("Static variable address %p\n", &si);
// Static variable address 0x100008010
print_address();
// print_address()
// Static function address 0x100003ea0
// Static valiable address 0x100008014
return 0;
}
printf("\nmain()\n");
// main()
printf("Static function address %p\n", multiply);
// Static function address 0x100003de0
printf("Static variable address %p\n", &si);
// Static variable address 0x100008010
print_address();
// print_address()
// Static function address 0x100003ea0
// Static valiable address 0x100008014
- my_functions.h 에서 static 으로 선언한 변수 si 와 함수 multiply 호출 시, 호출 방법에 따라 다른 메모리 주소에 할당되는 것을 확인 할 수 있음
- static 으로 선언 시, 함수가 끝나도 메모리에서 사라지지 않고 유지 됨
이 글의 모든 사진과 내용의 출처는 홍정모 교수님께 있음을 알려드립니다.
http://blog.naver.com/atelierjpro
실리콘 밸리의 프로그래머 : 네이버 블로그
안녕하세요! 홍정모 블로그에 오신 것을 환영합니다. 주로 프로그래밍 관련 메모 용도로 사용합니다. 강의 수강하시는 분들은 홍정모 연구소 카페로 오세요.
blog.naver.com
http://www.inflearn.com/course/following-c
홍정모의 따라하며 배우는 C언어 - 인프런 | 강의
'따배씨++'의 성원에 힘입어 새롭게 개발된 C 언어로 시작하는 프로그래밍 입문 강의입니다. '따배씨'와 함께 프로그래밍 인생을 업그레이드 해보세요., 따라하며 배우는 C언어 '따배씨++'의 성원
www.inflearn.com