C언어 다음의 차이가 뭘까요;;

serefe1025의 이미지

안녕하세요.
C언어 공부중인 컴공 학생인데요...
밑부분에 printf("\n"); 란게 있는데요.
이게 int i; 요거의 밑에 있을때는 컴파일 에러가 안뜨는데
int i; 위에 있을때는 컴파일 에러가 뜹니다.;;
그냥 무의미한 줄바꿈 명령어인데 왜 이런 현상이 발생하는걸까요...

#include

typedef struct student {
char name[20];
int korean, english, math;
double average;
} STUDENT;

int main(void)
{
STUDENT s1 = {"김모모", 100, 100, 100, 0.0};
STUDENT s2 = {"박나나", 90, 80, 88, 0.0};
STUDENT s3 = {"이만수", 45, 32, 44, 0.0};
STUDENT *std[] = {&s1, &s2, &s3};

//int i;의 윗줄인 이 부분에 있을때는 컴파일 에러가 뜹니다.
int i;
printf("\n"); //요부분 입니다. 여기 있을때는 컴파일 에러가 안뜹니다.

for(i=0; i<3; i++)
(*std[i]).average = ((*std[i]).korean + (*std[i]).english + (*std[i]).math)/3;

for(i=0; i<3; i++)
printf("%s %f\n", std[i]->name, std[i]->average);

return 0;
}

익명 사용자의 이미지

C의 이전 표준이 지역변수 선언은 함수의 앞쪽에서 다 하게 되어 있습니다.

printf("\n");같은 문장이 나왔다는건
앞쪽은 끝나고 함수의 본문이 시작된다는 얘기고
함수 본문에서 지역변수를 선언하니 오류라고 나오는거죠.

serefe1025의 이미지

요거땜에 생각하느라 골치아팠는데

정말 감사합니다.^^

익명 사용자의 이미지

최근 표준을 사용하도록 컴파일 옵션을 주시면 printf 가 위에 있어도 에러가 나지 않을겁니다.
예를 들어 gcc라면 컴파일시에 -std=c99 옵션을 주면됩니다. visual c 역시 비슷한 옵션이 있으니 찾아보세요.

a287848의 이미지

function prologue 가 끝난 후 stack 에 local 변수가 들어갈 자리를 확보하려는 의도는 알겠는데... 어차피 local 변수 사이즈는 esp에 변수 사이즈 만큼 빼면 될텐데...

저는 저 의도가 혹시 return 값을 stack 으로 받을 수 있는 architecture 가 있어서 그런건 아닐지 라는 생각을 하곤 합니다. 그러면 function return 이후 stack 에 불필요한 값이 있을테니깐요.

잘 아시는분 계신가요?

Dig it.

댓글 달기

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