C에서 연결이란?

프로그래밍/Embedded 최적화 과정 2007/01/24 22:24 게으른 엔지니어
연결이란 아래와 같이 생각할 수 있다. 요전에 포스팅한 통용범위와 많은 관련이 있는데, 연결이란 아래와 같이 세가지 종류가 있다.

1. 내부 연결 : static, 파일
2. 외부연결
3. 연결 없음

선언은 여러개가 있을수 있지만, 정의는 하나만 존재 해야 한다.
함수는 정의가 곧 선언이다.

1. extern이 있을때
   1.1. 선언 존재
      1.1.1. 연결 있음 : 연결 물려 받음
      1.1.2. 연결 없음 : 외부 연결
   1.2. 처음 선언 : 외부 연결

2. extern이 없을때
   2.1. 함수 : extern가정
   2.2. 변수(대상체, 메모리상에 존재하는 대상)
      2.2.1. 파일 : 외부 연결
      2.2.2. 블록 : 연결 없는 새로운 명칭

 

<----------- 외부연결 --------->
<--- 내부연결 ------>
a.c ------------> a.o
b.c ------------> b.o   ===> 링커 ===> a.out
c.c ------------> c.o

 
[code type=c]#include <stdio.h>

static int a; // 파일 통용범위 내부 연결
int b; // 외부 연결
extern int c; // 외부 연결
extern int d; // 외부 연결

int main(void)
{
 int x; // 연결없는 새로운 명칭
 extern int y; // 외부 연결
 extern int a; // 내부 연결 물려 받음. 3번 line에서 이미 선언.
 int b; // 연결없는 새로운 명칭
 extern int c; // 외부 연결. line 5의 것을 물려 받음.
 int d; // 연결없는 새로운 명칭

 return 0;
}

extern int a; // 내부 연결. line 3의 연결 물려 받음.
static int a; // 내부 연결[/code]

아래 코드에서 결과가 100 이 나오는 이유를 생각해 보자.

[code type=c]#include <stdio.h>

int a = 100; // 외부
int main(void)
{
 int a = 200; // 연결 없음. 새로운 명칭
 {
  int a = 300; // 연결 없음. 새로운 명칭
  {
   extern int a; // 외부
   printf("aa = %d\n", a);
  }
 }

 return 0;
}[/code]
 
크리에이티브 커먼즈 라이센스
Creative Commons License
이올린에 북마크하기(0) 이올린에 추천하기(0)
TAG ,
받은 트랙백이 없고, 댓글이 없습니다.

댓글+트랙백 RSS :: http://www.cipher.pe.kr/tt/cipher/rss/response/124

댓글+트랙백 ATOM :: http://www.cipher.pe.kr/tt/cipher/atom/response/124

트랙백 주소 :: http://www.cipher.pe.kr/tt/cipher/trackback/124

트랙백 RSS :: http://www.cipher.pe.kr/tt/cipher/rss/trackback/124

트랙백 ATOM :: http://www.cipher.pe.kr/tt/cipher/atom/trackback/124

댓글을 달아 주세요

댓글 RSS 주소 : http://www.cipher.pe.kr/tt/cipher/rss/comment/124
댓글 ATOM 주소 : http://www.cipher.pe.kr/tt/cipher/atom/comment/124
[로그인][오픈아이디란?]