18
#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);
}

我正在尝试将一个变量添加到自身,然后将该变量打印出来,但我似乎无法让我的代码正常工作。

我的输出错误是

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]
4

2 回答 2

32

printf需要一个格式字符串:

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

参考页面提供了有关如何使用printf和相关功能的详细信息。

于 2013-05-21T16:46:41.920 回答
2

正如Shafik 已经写的那样,你需要使用正确的格式,因为scanf你会得到一个字符。

如果您不确定其用法,请随时查看printf - C++ Reference 。

提示:写起来更快/更好x = x + 1;较短的方法是:x++;

于 2013-05-21T16:53:18.347 回答