전역변수에 대한 질문입니다

ch0nn0m의 이미지

#include<stdio.h>
char buf[20];
int main()
{
        sprintf(buf,"%s","success");
        printf("%s\n",buf);
        printf("%d\n",strlen(buf));
 
        sprintf(buf,"%s","Fail");
        printf("%s\n",buf);
        printf("%d\n",strlen(buf));
}

이렇게 했을경우

success
7
Fail
4
이렇게 나옵니다...


궁금한게 2가지가 있는데요..

1) 전역변수는 다른 함수내에서 초기화가 가능한지요...??

2) 위와같이 할경우 전역변수 buf에는 success가 들어간뒤 fail이 들어가게 되는데...failess와 같이 나와야하는게 아닌지요??

select99의 이미지

1질문은 물론 가능하구요.

2는.. \n 을 넣었을뿐만아니라..

스트링이기때문에 안나오죠..

님이 예상하신 결과는 memcpy 했을때 나옵니다.

Risty의 이미지

sprintf 명령에서는 대상 버퍼에 들어가는 생성 결과 마지막에 널 문자('\0')를 덧붙이게 되어 있습니다.

klutzy의 이미지

1. 전역변수는 아무 함수에서나 쓸 수 있습니다. 편리하기는 하지만 남발하면 코드를 엉망으로 만드는 원인이기도 하니 조심해서 써야 합니다.

참고로 전역 변수는 초기화하지 않아도 항상 0으로 초기화됩니다. 이 성질은 2번과도 어느정도 관계가 있습니다.

2.
sprintf(buf,"%s","success");
buf == {'s', 'u', 'c', 'c', 'e', 's', 's', '\0', '\0', ... }

sprintf(buf,"%s","Fail");
buf == {'F', 'a', 'i', 'l', '\0', 's', 's', '\0', '\0', ... }

C의 문자열은 항상 마지막에 \0이 붙습니다. "Fail" 등의 큰따옴표 문법은 사실 {'F', 'a', 'i', 'l', '\0'}의 5개짜리 문자를 편의적으로 줄인 것이고요. 의미상으로는 4개지만 메모리상으로는 5개입니다.

sprintf의 경우 \0을 떼야 할 때는 떼고 붙여야 할 때는 붙입니다. 예를 들어,
sprintf(buf,"%s","success");
sprintf(buf,"%s%s","Fail", "x");
이렇게 했다면
buf == {'F', 'a', 'i', 'l', 'x', \0', 's', '\0', '\0', ... }
이 됩니다. \0은 단순히 문자열의 끝을 알리는 값이기 때문에 이렇게 동작하게 됩니다.

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.