ç为size_t和ssize_t供负值 [英] C size_t and ssize_t negative value

查看:258
本文介绍了ç为size_t和ssize_t供负值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为size_t 声明为 unsigned int类型,因此它不能再present负值。
因此,有 ssize_t供这是签署的类型为size_t 吧?结果
这里是我的问题:

size_t is declared as unsigned int so it can't represent negative value.
So there is ssize_t which is the signed type of size_t right?
Here's my problem:

#include <stdio.h>
#include <sys/types.h>

int main(){
size_t a = -25;
ssize_t b = -30;
printf("%zu\n%zu\n", a, b);
return 0;
}

为什么我的了:

18446744073709551591
18446744073709551586

作为结果呢?结果我知道,与为size_t 这可能是可能的,因为它是一个无符号的类型,但为什么我得到一个错误的结果也与 ssize_t供 ??

as result?
I know that with size_t this could be possible because it is an unsigned type but why i got a wrong result also with ssize_t??

推荐答案

在你分配给无符号类型第一案 - A 。在第二种情况下,你使用了错误的格式说明。第二个符应%ZD 而不是%祖

In the first case you're assigning to an unsigned type - a. In the second case you're using the wrong format specifier. The second specifier should be %zd instead of %zu.

这篇关于ç为size_t和ssize_t供负值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆