static 키워드에 대한 질문..

익명 사용자의 이미지


안녕하세요..

요즘 프로그래밍을 하다가 static 키워드에 대해서
자주 보게 되는데요...

이 static 키워드에 대해서 잘 이해가 안가서 이렇게 질문을
올립니다..

이 키워드가 사용되는게 변수랑 함수에도 사용되는데
이것이 붙고 안 붙고에 따라서 어떤 차이가 있나요??

책에 변수에 대한 것이 나와 있지만, 솔직히 잘 이해가 가질 않고,
함수에 대한 것은 거진 나와 있는 것이 없군요...

질문을 다시 정리 하자면..
1. 변수에 static 이 붙고 안붙고의 차이..
2. 함수에 static 이 붙고 안붙고의 차이..

에 대해서 알려 주시면 감사하겠습니다..

현재 하는 일이 포팅 작업을 하는데, static 키워드가 자주
눈에 띄어서 이전부터 가지고 있던 의문을 이번에 풀었으면
하는 바램에 이렇게 글을 올립니다..

그럼 오늘도 좋은 하루 되세요...

익명 사용자의 이미지

C언어 프로그래밍시 분리컴파일 (소스코드를 여러 파일로 나누어서 컴파
일) 할때 static이나 extern등을 사용하죠.
예를 들어, hello.c, hello2.c 이렇게 소스가 두개 있을 때

hello.c
-------------------------------
extern g_value; /* hello2.c 파일에서 정의된 외부변수 선언 */
static char* s_hello="Hello??";
...
static void s_func()
{
printf("hello.c의 static함수 \n");
}

int main(void)
{
printf("%d \n", g_value);
sfunc();
}
-------------------------------

hello2.c
-------------------------------
int g_value = 100;
static char* s_hello="안녕하세요?";

static void s_func()
{
printf("hello.c의 static함수 \n");
}
-------------------------------

이거 컴파일은 일케 하겠죠.
gcc -c hello.c
gcc -c hello2.c
gcc hello.o hello2.o -o hello

C에서의 함수는 전역이라 이름이 똑같으면 안되는데, s_func는 hello.c
hello2.에서 둘다 같지만, 링크시 이름충돌이 안생겨요. 즉 함수의 이름공
간영역이 파일안으로 제한이 되죠. 물론 static 전역변수의 경우도 마찬가
지 이구요.
단 전역변수는 한 파일에서 정의(메모리공간에 할당)했으면 다른 파일에서
는 extern으로 외부변수 형태로 선언해서 사용해야 합니다. 안그러면, 링
크시 똑같은 심벌이 두개 있다고, 링커가 불평(?)을 하죠~ ^^

익명 사용자의 이미지

static을 쓰는 예제중 가장 흔히 볼 수 있는(??)코드가...음...
다음과 같은 소스들이겠죠??..

void add()
{
static i = 0;
i++;
print("%d\n", i );
}

main()
{
int c = 0;
for( c = 0 ; c < 5 ; c++ )
{
add();
}
}

함수프로토랑 include는 귀찮아서 뺐습니다..
출력결과는 당근
1
2
3
4
5

가 돼겠죠?
음..변수는 이래도..함수는..위에분이 말씀하신
전역과...범위(??)를 위한 용도 외에...잘 모르겠네요..
자주 호출되는 함수는 static으로 하면 메모리에 항상 상주해서 속도가 좋다(??)..라도 있을려나?

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.