【问题标题】:GCC compiler warning: format ‘%c’ expects argument of type ‘char *’, but argument 2 has type ‘int *’ [-Wformat]GCC 编译器警告:格式“%c”需要“char *”类型的参数,但参数 2 的类型为“int *”[-Wformat]
【发布时间】:2012-03-10 07:56:32
【问题描述】:

当我尝试编译代码时收到以下警告:

exercise6.c:32:14:警告:格式“%c”需要“char *”类型的参数,但参数 2 的类型为“int *”[-Wformat]

是什么导致了这个警告,我该如何解决?

/*Write a program that displays the contents of a file at the terminal 20 lines at
a time. At the end of each 20 lines, have the program wait for a character to be
entered from the terminal. If the character is the letter q, the program should
stop the display of the file; any other character should cause the next 20 lines
from the file to be displayed.*/

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

int main (void)
{
    int c, i;
    FILE *file;

    if ( (file = fopen ("text", "r")) == NULL )
    printf ("Error opening the file.\n");

    for ( i = 0; i < 20; ) {
        c = getc (file);

        if ( c == EOF ) {
            fclose (file);
            exit (EXIT_SUCCESS);
        }

        putc (c, stdout);

        if ( c == '\n' )
            ++i;

        if ( i == 20 ) {
            scanf ("%c", &c);
            if ( c == 'q' ) {
                fclose (file);
                exit (EXIT_SUCCESS);
            }
            i = 0;
        }
    }
}

【问题讨论】:

    标签: c gcc-warning


    【解决方案1】:

    定义一个char ch 并在scanf 中使用它。

    int c, i;
    char ch;
    
    /* ... */
    scanf ("%c", &ch);
    

    scanf 使用不匹配的参数在技术上是未定义的行为。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-22
      • 2015-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多