简体   繁体   English

C 中的无符号长

[英]Unsigned long in C

I try to put an unsigned INT just to play around with it but it don't seems to work I don't see where the problem could be here is the code :我尝试放置一个未签名的 INT 只是为了玩弄它,但它似乎不起作用我不知道问题可能出在哪里是代码:

#include <stdio.h>

void main()
{
     unsigned long nou=2200000000UL;
     
    printf("the number is %d" ,nou);

}

It return : the number is -2094967296 as The maximum value of INT = 2147483647 it's normal it don't give the good output but when I try with :它返回: the number is -2094967296作为 INT 的最大值 = 2147483647 这是正常的,它没有给出好的输出,但是当我尝试使用时:

  #include <stdio.h>
    
    void main()
    {
         unsigned long nou=2100000000;
         
            printf("the number is %d" ,nou);
    
    } 

The correct value gets print out , I don't know why the unsigned command don't work .打印出正确的值,我不知道为什么 unsigned 命令不起作用。

You should change %d in the printf call to %u (and %lu when using unsigned long ).您应该在 printf 调用中将%d更改为%u (和%lu在使用unsigned long )。

According to the application binary interface on most systems, signed and unsigned integers are both passed in general purpose registers whenever possible.根据大多数系统上的应用程序二进制接口,只要有可能,有符号和无符号整数都会在通用寄存器中传递。 Their signedness is determined by the subroutine that recieves them.它们的签名由接收它们的子程序确定。

  • %d and %i are decimal output for **signed** int ; %d%i**signed** int十进制输出;

  • %u is the decimal output for **unsigned** int ; %u**unsigned** int的十进制输出;

  • %o , %x , and %X are octal, hexadecimal lowercase, and hexadecimal uppercase output for unsigned integers as well. %o%x%X也是无符号整数的八进制、十六进制小写和十六进制大写输出。


Appendix附录

As noted in the comments, there are other flags and modifier letters needed for integers whose ranks are greater than int , integers whose ranks are lesser than int are promoted in variadic functions.正如在评论中指出,还有其他的标志和需要,其等级被大于整数修饰符字母int ,整数,其等级被比较小int在可变参数的功能被提升。

There are some good online sources to consult for the usage of printf.有一些很好的在线资源可以参考 printf 的用法。

  • cplusplus.com and cppreference.com and alikes are good for beginners, cplusplus.com 和 cppreference.com 等对初学者有好处,

  • ISO C drafts are good when you need a free reference as close to authoritative as possible.当您需要尽可能接近权威的免费参考时,ISO C 草案是很好的选择。 They are available at http://open-std.org/JTC1/SC22/WG14/www/projects它们可以在http://open-std.org/JTC1/SC22/WG14/www/projects 上找到

  • The POSIX standards and man page on various Unix and Unix-like systems contain extansions that are recognized in these operating system environments.各种 Unix 和类 Unix 系统上的 POSIX 标准和手册页包含在这些操作系统环境中可识别的扩展。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM