c - 常量与变量不匹配

标签 c linux

有人能解释一下为什么常数与if()条件中的变量不匹配吗?

z3 tests # cat deftst.c
#define _GNU_SOURCE
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <linux/fs.h>
#include <fcntl.h>

#define MY_BLOCK_SIZE               512

main() {
        int fd,ret;
        size_t block_size;

        fd=open("/dev/loop2",O_DIRECT|O_RDWR|O_NONBLOCK);
        printf("fd=%d\n",fd);

        ret = ioctl(fd, BLKSSZGET, &block_size);
        printf("ret=%d\n",ret);
        if (block_size!=MY_BLOCK_SIZE) {
                printf("block_size=%d , MY_BLOCK_SIZE=%d\n",block_size,MY_BLOCK_SIZE);
                printf("error, block size don't match the constant!\n");
        }
}
z3 tests # gcc -o deftst deftst.c
deftst.c: In function 'main':
deftst.c:21:3: warning: format '%d' expects type 'int', but argument 2 has type 'size_t'
z3 tests # dd if=/dev/zero of=device.dat bs=512 count=128
128+0 records in
128+0 records out
65536 bytes (66 kB) copied, 0.00167074 s, 39.2 MB/s
z3 tests # losetup /dev/loop2 device.dat
z3 tests # ./deftst
fd=3
ret=0
block_size=512 , MY_BLOCK_SIZE=512
error, block size don't match the constant!
z3 tests #

我试着用十六进制打印,并确认它在常量和变量中都是相同的值。真奇怪。

最佳答案

似乎blksszget需要一个指向int的指针,您的块大小可能很长。见here

关于c - 常量与变量不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21092228/

相关文章:

c - 获取C中设置位的位置

c - 我不断看到 const char * const 参数,我知道第二个 const 对于堆栈复制毫无意义,那么他们为什么要这样做呢?

c - 未对齐数据的性能损失

Linux:找出最近更新的前 10 个文件/文件夹

c - 在 C 中定义常量数组

c++ - 打包修整的 C 结构

c - GNU C 中的激活记录(嵌套函数)

linux - 从批处理中终止交互式程序

linux - 创建一个文本文件来保存 etc/init.d 的多个服务

PHP 作为守护进程/服务