2014-09-17 5 views
0

나는 전화 기능을 배우려고 노력 중이며 a, b, cd을 선언해야한다고 들었지만 프로그램의 요점은 사용자에게 묻는 것입니다. 이 숫자들을 합한 다음 합계하십시오.변수를 입력하라는 메시지가 나타나면 어떻게 선언합니까?

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <math.h> 

int f(int a, int b, int c, int d); 
int g(int b, int c, int d); 
int h(int c, int d); 
int i(int d); 

int 
main(int argc,char **argv) 
{ 
     int result; 
     result = f(a,b,c,d); 
     printf("Value of a?"); 
     scanf("%d",a); 
     printf("Value of b?"); 
     scanf("%d",b); 
     printf("Value of c?"); 
     scanf("%d",c); 
     printf("Value of d?"); 
     scanf("%d",d); 
     printf("Your result is %d",result); 
     return 0; 
} 

int 
f(int a, int b, int c, int d) 
{ 
    return a + g(b,c,d); 
} 

int 
g(int b, int c, int d) 
{ 
     return b + h(c,d); 
} 

int 
h(int c, int d) 
{ 
     return c + i(d); 
} 

int 
i(int d) 
{ 
     return d + d; 
} 

특정 경고

call.c:16:13: error: ‘a’ undeclared (first use in this function) 
     result = f(a,b,c,d); 

이며 B, C 및 D에 대해 반복한다.

누구든지 내가 잘못한 것을 말할 수 있습니까? int a, int b, int cint d이 이미 정의 된 위치에 함수 서명을 넣었으므로 잘못 입력했는지 혼동합니다.

편집 : 질문이 해결되었습니다. 코드는 당신은,

int result, a, b, c, d;

는 또한 scanf 그것에 포인터 인수의 가변 부분의 기대

에 코드를 변경하려고 그래서 '주에서 그 변수를 선언 할 필요가

 int result; 
     int a; 
     int b; 
     int c; 
     int d; 
     printf("Value of a?"); 
     scanf("%d",&a); 
     printf("Value of b?"); 
     scanf("%d",&b); 
     printf("Value of c?"); 
     scanf("%d",&c); 
     printf("Value of d?"); 
     scanf("%d",&d); 
     result = f(a,b,c,d); 
     printf("Your result is %d",result); 
     return 0; 
+0

'int result' 바로 다음에 선언하십시오. –

+0

참고로 변수/함수 이름을 더 잘 설명하십시오. –

+0

함수 f의 매개 변수는 main의 a, b, c, d와 관련이 없습니다. 당신은 그 (것)들을 w, x, y, z 또는 무엇이든이라고 부를 수 있었다. 그들은 main에서 선언되어야합니다. –

답변

1

과 같아야합니다 & 참조 연산자를 사용하여 a의 주소를 가져야합니다.

예 : scanf("%d", &a); 모든 scanf 통화에 대해이 작업을 수행하십시오.

관련 문제