2015-01-04 4 views
2

C Programming Absolute Beginner's Guide이라는 책에서 C를 배우려고했는데 문제가 생겼습니다. 나쁜 점은 질문 할 수 없다는 것입니다. 책 질문! Google에 오류를 입력하면 나를이 사이트로 안내했습니다. 내가받는 오류는 질문 제목에 있으며 이는 내 코드입니다.오류 : 예상 선언자 또는 '{'토큰 앞에 '...'토큰이 있습니다.

#include <stdio.h> 

main(

{ 

//Set up the variables, as well as define a few 

    char firstInitial, middleInitial; 
    int number_of_pencils; 
    int number_of_notebooks; 
    float pencils = 0.23; 
    float notebooks = 2.89; 
    float lunchbox = 4.99; 


    //The information for the first child 
    firstInitial = 'J'; 
    middleInitial = 'R'; 

    number_of_pencils = 7; 
    number_of_notebooks = 4; 

    printf("%c%c needs %d pencils, %d notebooks, and 1 lunchbox\n", 
     firstInitial, middleInitial,number_of_pencils, 
     number_of_notebooks); 
    printf("The total cost is £%.2f\n\n", number_of_pencils*pencils + number_of_notebooks*notebooks + lunchbox); 

    //The information for the second child 
    firstInitial ='A'; 
    middleInitial = 'J'; 

    number_of_pencils = 10; 
    number_of_notebooks = 3; 

    printf("%c%c needs %d pencils, %d notebooks, and 1 lunchbox\n", 
     firstInitial, middleInitial,number_of_pencils, 
     number_of_notebooks); 
    printf("The total cost is £%.2f\n\n", number_of_pencils*pencills 
     + number_of_notebooks*notebooks + lunchbox); 

    //The information for the third child 
    firstInitial = 'M'; 
    middleInitial = 'T'; 

    number_of_pencils = 9; 
    number_of_notebooks = 2; 

    printf("%c%c needs %d pencils, %d notebooks, and 1 lunchbox\n", 
     firstInitial, middleInitial,number_of_pencils, 
     number_of_notebooks); 
    printf("The total cost is £%.2f\n", 
     number_of_pencils8pencils + number_of_notebooks*notebooks + lunchbox); 

     return0; 
} 

) 

이 코드의 잘못된 점은 무엇입니까?

main(
{ 

및 종료 :

+1

에 오신 것을 환영처럼 보일 것입니다. 곧 [About] 페이지를 읽으십시오. 컴파일 오류가 발생하면 정확한 오류 메시지를 포함하는 것이 가장 좋습니다. 단, 파일 이름이 마일 길이가 긴 경로 인 경우 이름을 파일의 기본 이름으로 줄이십시오 (따라서'/ huge/long/path/with/many/levels/of/directory/src/file.o'에서'file.o'까지). 표시하는 소스와 그 안에있는 행 번호가 컴파일러에서 말하는 것과 일치하는지 확인하십시오. 파일이 너무 길면 편안함을 느낄 수 있습니다. MCVE ([Minimal, Complete, Verifiable Example? (http://stackoverflow.com/help/mcve)를 만드는 방법) –

+1

나는 마지막 문자')' – Rizier123

+0

나는 그 책의 샘플 자료를 보았는데, 그 책의 내용을 보았을 때, (2 장에서) 샘플 프로그램은 모두'int main()'(혹은'int main (void)')보다는'main()'을 가지고있다. 'return 0;을 생략 할 수있는 C99 표기법을 사용하지 않았기 때문에 필요하다. (나는 그렇게하는 코드를 좋아하지 않지만) 제 3 판은 2014 일자로되어있다. 첫 번째 프로그램은 현재 C 언어로 약 15 년 동안 쓰여지지 않았지만,'return','while','int','if'','float''을'commands '로 특성화하는 것을 싫어합니다. 키워드 : 키워드 : –

답변

1

귀하의 main() 기능이 시작

} 
) 

이것은 잘못된 것입니다. 다음과 같아야합니다.

int main(void) 
{ 
    …body of function… 
} 

void은 선택 사항입니다. 반환 형식은 현대 C에서는 선택 사항이 아닙니다 (C89/C90 표준은 선택 사항이므로 C99 이상이 필요하므로 컴파일러가 고집하지 않더라도 필요한 경우 프로그래밍해야합니다). 올바른 반환 유형은 int입니다 (자세한 내용은 What should main return in C and C++? 참조).

또한, Rizier123pointed out으로, 이 main()의 끝에있다. 그 값은 return 0;이어야합니다.

다른 오류가 있는지 보려면 코드를 컴파일하지 않았지만 괄호와 중괄호를 잘못 처리하면 초기 오류가 발생합니다.

+3

또한 그는'return0;'에 공백을 추가해야만했습니다. – Rizier123

2

주요 기능이 좋지 않습니다. 컴파일러가 말합니다.

그것은

main() 
{ 
.... 
} 

대신

스택 오버플로
main(
{ 
... 
} 
) 
+2

현대 C (2000 년 이후로 작성된 모든 것)에서'main()'함수는 컴파일러가 실제로 그것을 주장하거나하지 않습니다. 모든 함수는 명시 적 리턴 유형을 가져야합니다. –

관련 문제