这些变量是哪些C语言的数据类型?
现在我正在学习 c语言,并且有两个任务。定义变量并在此处打印是我的代码:
#include <stdio.h>
int main()
{
printf("Excersise 1\n\n");
int cVarOne = -250;
int cVarTwo = 250;
int cVarThree = 4589498;
double cVarFour = 10000000000000000000;
double cVarFive = -9000000000000000000;
printf("%d\n%d\n%d\n%.0lf\n%.0lf\n\n", cVarOne,cVarTwo,cVarThree,cVarFour,cVarFive);
printf("Excersise Two\n\n");
unsigned short cVarSix = 43112;
int cVarSeven = -1357674;
int cVarEight = 1357674;
int cVarNine = -1357674000;
unsigned int cVarTen = 3657895000;
printf("%u\n%d\n%d\n%d\n%lu\n", cVarSeven, cVarEight, cVarNine, cVarTen);
return 0;
}
问题是要为练习1出现以下消息:
和练习两个相同的数字设置为变量,并未预测打印。这是结果:
我尝试使用不同的类型,但没有结果。我在哪里错了?应该使用哪些数据类型和指定符,为什么会发生这些错误?
我正在使用此在线编译器: https://www.onlinegdb.com/onlinegdb.com/online_c_compiler
感谢。
Now I am learning the C language and I have two tasks. Define variables and print them Here is my code:
#include <stdio.h>
int main()
{
printf("Excersise 1\n\n");
int cVarOne = -250;
int cVarTwo = 250;
int cVarThree = 4589498;
double cVarFour = 10000000000000000000;
double cVarFive = -9000000000000000000;
printf("%d\n%d\n%d\n%.0lf\n%.0lf\n\n", cVarOne,cVarTwo,cVarThree,cVarFour,cVarFive);
printf("Excersise Two\n\n");
unsigned short cVarSix = 43112;
int cVarSeven = -1357674;
int cVarEight = 1357674;
int cVarNine = -1357674000;
unsigned int cVarTen = 3657895000;
printf("%u\n%d\n%d\n%d\n%lu\n", cVarSeven, cVarEight, cVarNine, cVarTen);
return 0;
}
The problem is that for exercise 1 the following message appears:
and for exercise two the same numbers that are set as variables are not printed propetly. Here is the result:
I tried to use different types, but without result. Where am I wrong? What data types and specifiers should be used and why do these errors occur?
I am using this online compiler: https://www.onlinegdb.com/online_c_compiler
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
练习2中的警告是自我解释的,简单地使用正确的转换说明符,以供无符号INT,如您最喜欢的C书的第1章所教授的那样。该答案的其余部分是关于练习1:
为什么要警告:
整数常数,这些内容:
123
,具有类似于变量的类型。10000000000000000000
不是浮点类型,因为它不会以。
。 float),但一个固定点签名的整数。编译器将尝试找到该值最小的整数类型,但根本找不到一个。原因是因为10,000,000,000,000,000,000 = 10^19比最大的64位整数大,只能保持2^63-1 = 9.22*10^18。
但是,编译器指出,该值可能适合64位无符号整数,因为2^64 -1 = 18.44*10^18,足够大。因此,它尝试了一下,但会给您警告编写这种臭代码。
解决方案:
使用
1000000000000000000000.0
类型double> double
。或者更好,请使用
10E18
,它也是double
,但人类可读。一般最佳实践:
float
和double
在同一表达式中。 (请远离float
完全在PC等大多数主流系统上。)int
,long
/code>等。代替使用
stdint.h
的类型。The warning in Exercise 2 is self-explanatory, simple use the correct conversion specifier for unsigned int, as taught in chapter 1 of your favourite C book. The rest of this answer is regarding Exercise 1:
Why you get the warning:
Integer constants, these things:
123
, have a type just like variables.10000000000000000000
is not a floating point type since it doesn't end with.
(double
) or.f
(float), but a fixed point signed integer. The compiler will try to find the smallest signed integer type where the value will fit, but it fails to find one at all.The reason is because 10,000,000,000,000,000,000 = 10^19 is larger than the largest signed 64 bit integer, which can only hold values up to 2^63 - 1 = 9.22*10^18.
The compiler notes however that the value might fit in a 64 bit unsigned integer since 2^64 -1 = 18.44*10^18, large enough. Hence it tries that, but gives you the warning for writing such smelly code.
Solution:
Use
10000000000000000000.0
which is of typedouble
.Or better yet, use
10e18
which is alsodouble
but readable by humans.General best practices:
float
anddouble
in the same expression. (Stay clear offloat
entirely on most mainstream systems like PC.)int
,long
etc. Use the types fromstdint.h
instead.每种整数类型在字节中具有固定的大小,用于值的内部表示。结果,可以将最大值和最小值的限制存储在整数类型中。
例如,对于类型
uintmax_t
,可以由编译器提供以下。
特征 常数的第一个签名整数类型,可以代表这样的常数。但是,两项签名的整数类型都不包括
长长int
可以表示此常数。因此,编译器会发出一条消息,即这样的常数只能以无符号整数类型表示。您可以使用像后缀这样的后缀使用
常数确定常数具有类型
无签名的长长int
。至于第二个消息,您必须使用参数正确的转换说明符。
转换说明器
lu
旨在输出类型无签名的长int
的对象的值。但是,printf的呼叫的相应参数具有类型
unigned int
,因此要么使用转换规范符
u
喜欢或将参数施加到类型
nosigned ling
也喜欢此格式规范的长度修饰符
L
%。0LF
没有效果。您可以写%。0F
Each integer type has a fixed size in bytes for internal representations of values. As a result there is a limit for the maximum and a minimum value that can be stored in an integer type.
For example for the type
uintmax_t
the following characteristics can be provided by the compilerThe program output is
When you are using integer constant without suffix like this
10000000000000000000
then the compiler selects the type of the constant the first signed integer type that can represent such a constant. However neither signed integer type includinglong long int
can represent this constant. So the compiler issues a message that such a constant can be represented only in an unsigned integer type.You could use a constant with a suffix like
The suffix determines that the constant has the type
unsigned long long int
.As for the second message then you have to use correct conversion specifiers with arguments.
The conversion specifier
lu
is designed to output values of objects of the typeunsigned long int
. However the corresponding argument of the call of printfhas the type
unsigned int
So either use the conversion specifier
u
likeor cast the argument to the type
unsigned ling
likeAlso the length modifier
l
in this format specification%.0lf
has no effect. You could just write%.0f