在 if 条件下使用 clang 编译错误

Compiling errors using clang for if conditions

所以,我写下这段代码,主要目的是从存储卡中检索 JPEG。

这是我写到现在的内容。但是,在编译时,它在条件上给我错误。

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

int main(int argc, char *argv[])
{
    //Check for only the presence of 1 input to run
    if (argc != 2)
    {
        printf("Usage of only 1 command line argument");
        return 1;
    }
    //Name of the file storage in "name"
    char *name = argv[1];
    //As we know is 512B, give that specific syze
    char block[512];
    //Open file
    FILE *original = fopen (name, "r");
    //check if it has anything in it
    if (original == NULL)
    {
        printf ("The file has nothing on it\n");
        return 1;
    }
    //Variables to work with
    FILE *copia;
    char jpeg[8];
    int count = 0;
    //while reading its content
    while (fread (block, 512, 1, original) == 1)
    {
        //condition for the beggining of the jpeg
        if (block[0] == 0xff && block[1] == 0xd8 && block[2]==0xff && (block[3] & 0xf0) == 0xe0)
        {
            //Case of the first jpeg
            if (count == 0)
            {
                sprintf (jpeg, "%03i.jpg", count);
                copia = fopen (jpeg, "w");
                fwrite (block, 512, 1, copia);
                count = count + 1;
                fclose (copia);
            }
            //Case for over
            if (count > 0)
            {
                sprintf (jpeg, "%03i.jpeg", count);
                count = count +1;
                copia = fopen (jpeg, "w");
                fwrite (block, 512, 1, copia);
                fclose(copia);
            }
        }
        //close file
        fclose(original);
        return 0;
    }
}

编译器消息:

clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow    recover.c  -lcrypt -lcs50 -lm -o recover
recover.c:32:22: error: result of comparison of constant 255 with expression of type 'char' is always false [-Werror,-Wtautological-constant-out-of-range-compare]
        if (block[0] == 0xff && block[1] == 0xd8 && block[2]==0xff && (block[3] & 0xf0) == 0xe0)
            ~~~~~~~~ ^  ~~~~


recover.c:32:42: error: result of comparison of constant 216 with expression of type 'char' is always false [-Werror,-Wtautological-constant-out-of-range-compare]
        if (block[0] == 0xff && block[1] == 0xd8 && block[2]==0xff && (block[3] & 0xf0) == 0xe0)
                                ~~~~~~~~ ^  ~~~~



recover.c:32:61: error: result of comparison of constant 255 with expression of type 'char' is always false [-Werror,-Wtautological-constant-out-of-range-compare]
        if (block[0] == 0xff && block[1] == 0xd8 && block[2]==0xff && (block[3] & 0xf0) == 0xe0)
                                                    ~~~~~~~~^ ~~~~
3 errors generated.
<builtin>: recipe for target 'recover' failed
make: *** [recover] Error 1

任何人都可以阐明这个主题吗?

三个-Wtautological-constant-out-of-range-compare warnings/errors是因为在block[0] == 0xff比较中,0xff是一个整数 (int) 常量,值为 255block[0] 是一个(有符号的)char,其范围为 -128127。因此,如消息所述,比较的结果将 始终 为假。 block[1] == 0xd8 测试也类似(0xd8216,对于 char 也是 out-of-range)。

有两种方法可以解决这个问题。第一个(不推荐)是在比较之前将常量显式转换为 char 值,如 if (block[0] == (char)0xff ... 等。

但更好的方法是对 block 数组使用 unsigned char 类型,因此请使用:

unsigned char block[512];

而不是:

char block[512];

第二种方法更准确地表示了 JPEG 文件的组成数据元素;它消除了警告,因为 unsigned char 的范围是 0255