C + XINU - 打印的int类型与鲁% [英] Printing int type with %lu - C+XINU
问题描述
我有一个给定的code,在我看来,有什么不对的code:
我编译下XINU。
I have a given code, in my opinion there is something wrong with that code: I compile under XINU.
接下来的变量是相关的:
The next variables are relevant :
unsigned long ularray[];
int num;
char str[100];
有一个函数返回INT:
There is a function returns int:
int func(int i)
{
return ularray[i];
}
现在的code是这样的:
now the code is like this:
num = func(i);
sprintf(str, "number = %lu\n", num);
printf(str);
问题是我得到大的数字,同时与陆%,这是不正确的打印。
The problem is I get big numbers while printing with %lu, which is not correct.
如果我改变的%禄为%d个后,我得到了正确的号码。
例如:用%lu个我得到27654342,同时与%D我得到26,后者是正确的;
If i change the %lu to %d, i get the correct number. For example: with %lu i get 27654342, while with %d i get 26, the latter is correct;
变量给出,该函数的声明中给出,我写的身体,但它必须返回INT;
The variables are given, the declaration of the function is given, i write the body but it must return int;
我的问题是:
-
我不熟悉的sprintf也许这个问题是存在的?
I'm not familiar with 'sprintf' maybe the problem is there?
我分配unsigned long类型为int,然后打印用%lu个整型,是正确的?
I assigned unsigned long to int and then print the int with %lu, is That correct?
我该如何解决这个问题呢?
How can i fix the problem?
先谢谢了。
谢谢大家的回答。
我只想说,我在工作XINU,以及我改变了文件的编译的顺序,你知道什么?其工作,并显示在%鲁%d个相同的数字。
Thanks everyone for answering. I just want to mention I'm working under XINU, well i changed the order of the compilation of the files and what you know... its working and showing same numbers on %lu and %d.
我清楚地知道,分配unsigned long类型为int,然后用%lu个印刷是不正确的编码,可能会导致数据丢失。
但正如我所说,code给定,我不能改变的变量和打印命令。
I'm well aware that assigning 'unsigned long' to int and then printing with %lu is incorrect coding and may result loss of data. But as i said, the code is given, i couldn't change the variables and the printing command.
我没有错误或警告BTW。
我不知道为什么改变固定的问题的编译顺序,如果有人有一个想法,你更然后欢迎大家分享。
I had no errors or warnings btw. I have no idea why changing the compilation order fixed the problem, if someone have an idea you are more then welcome to share.
我要感谢大家谁试图帮助我。
I want to thank all of you who tried to help me.
推荐答案
1)误解为普通格式说明
1) the misunderstanding is format specifiers in general
2)num是一个int - 因此,%d个
是正确的,当一个 INT
是你想要的打印。
2) num is an int -- therefore, %d
is correct when an int
is what you want to print.
3)理想
-
INT FUNC(int i)以
将无符号长FUNC(为size_t我)
- 和
INT NUM
将无符号长NUM
- 和
的sprintf(STR,数=%d个\\ N,NUM);
将的sprintf(STR,数=%lu个\\ n,NUM);
int func(int i)
would beunsigned long func(size_t i)
- and
int num
would beunsigned long num
- and
sprintf(str, "number = %d\n", num);
would besprintf(str, "number = %lu\n", num);
这样一来,就没有缩小,也没有转换 - 类型/值是$正确执行整个$ p pserved
that way, there would be no narrowing and no conversions -- the type/values would be correctly preserved throughout execution.
和是迂腐,的printf
应的printf(%S,STR);
如果你把你的警告级别一路上涨,你的编译器会警告你一些这些事情。我一直在编程很长一段时间,我还是离开了警戒线令人讨厌高(可以通过一些人的标准)。
if you turn your warning levels way up, your compiler will warn you of some of these things. i have been programming for a long time, and i still leave the warning level obnoxiously high (by some people's standards).
这篇关于C + XINU - 打印的int类型与鲁%的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!