2013-05-21 2 views
5
#include <stdio.h> 
#include <stdlib.h> 

int main() 
{ 
    int x = 1; 

    printf("please make a selection with your keyboard\n"); 
    sleep(1); 
    printf("1.\n"); 

    char input; 
    scanf ("%c", &input); 
    switch (input) { 
     case '1': 
      x=x+1; 
      printf(x); 
    } 

    return(0); 
} 

변수를 자체적으로 추가 한 다음 해당 변수를 출력하려고하지만 코드가 작동하지 않는 것 같습니다.printf 변수 C

printf("%d\n", x); 

reference pageprintf 및 관련 기능의 사용 방법에 대한 자세한 설명을 제공합니다

내 출력 오류가

newcode1.c: In function ‘main’: 
newcode1.c:20:2: warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast [enabled by default] 
In file included from newcode1.c:1:0: 
/usr/include/stdio.h:362:12: note: expected ‘const char * __restrict__’ but argument is of type ‘int’ 
newcode1.c:20:2: warning: format not a string literal and no format arguments [-Wformat-security] 
+0

처리 나에게 정말 유용했다, 당신은 변수를 인쇄되지 않는 경우, 당신은 일부 변수의 [현재] * 값 *을 출력합니다. –

+0

또한,'scanf '전에'input'을 초기화하고'scanf' 결과를 테스트하는 것이 더 좋습니다 –

답변

16

귀하의 printf는 형식 문자열을 필요로합니다.

+0

그게 전부입니다.!?!?! 고마워. – Dave

2

Shafik이 이미 쓴대로 scanf이 당신에게 숯을 가져 오기 때문에 올바른 형식을 사용해야합니다. 사용법에 대해 잘 모르는 경우 여기를 주저하지 마십시오. http://www.cplusplus.com/reference/cstdio/printf/

힌트 : 쓰기가 더 빠르거나 더 멋졌습니다. x=x+1; 짧은 방법 : x++; 그냥 대답 그에게 링크를주고 싶어 무슨 대답에 대한

죄송합니다 - 사이트가 모든 시간 C. BTW