为什么C中的输出不同? [英] Why are the outputs different in C?
本文介绍了为什么C中的输出不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include<stdio.h>
void main()
{
int a=010;
printf("\na=%d",a);
}
#include<stdio.h>
void main()
{
int a=010;
printf("\na=%o",a);
}
#include<stdio.h>
void main()
{
int a=010;
printf("\na=%x",a);
}
第一个节目的输出:8
第二个节目的输出:10
第三个节目的输出:8
因为我是初学者,我想知道原因在printf()中使用%之后的输出差异后面的差异。接下来我不明白变量中的数字是如何打印的,而其他值是打印出来的。
我不明白当二进制数等于2时,八进制的010 = 8如何。
请帮助我。谢谢。
Output for first program:8
Output for second program:10
Output for the third program:8
Since I am a beginner, I would like to know the reason behind the difference in ouput when using different letters after % in printf(). The next I don't understand how the number in the variable doesn't print and other values get printed.
I don't understand how 010=8 for octal when it is equal to 2 in binary.
Please help me. Thanks.
推荐答案
您要将变量设置为值010
这是十进制值<$的八进制c $ c> 8 。
案例1:打印为小数:8
案例2:打印为八进制:010
案例3:以十六进制打印:8
您需要去学习数字系统以了解它们的不同之处。
you are setting the variable to the value010
which is octal for the decimal value8
.
Case 1: print as decimal: 8
Case 2: print as octal: 010
Case 3: print as hexadecimal: 8
You need to go and study number systems to understand why they are different.
这些是不同格式的字符串表示形式:十进制,八进制和十六进制: http://www.cplusplus.com/reference / cstdio / printf [ ^ ]。
首先,C ++中的010几乎是误导性的:它实际上意味着小数为8.请参阅:http://en.cppreference.com/w/cpp/language/integer_literal [ ^ ]。
在某种程度上,所有输出都相同,只用不同基数的位置数字表示法表示。如果将格式更改为a =%d,a = octal 0%o和a = 0x%x,您可以轻松理解它。 :-)
-SA
These are string representations in different formats: decimal, octal and hexadecimal: http://www.cplusplus.com/reference/cstdio/printf[^].
First of all, 010 in C++ is pretty much misleading: it actually means decimal 8. Please see: http://en.cppreference.com/w/cpp/language/integer_literal[^].
In a way, all the outputs are identical, only expressed in positional numeric notations with different base. You may easily understand it if you change your formats to "a = %d", "a = octal 0%o" and "a = 0x%x". :-)
—SA
在所有三个程序中,变量a
包含数字8
。通过在开头添加0
,您已经使用了八进制文字 [ ^ ]。
In all three programs, the variablea
contains the number8
. By putting0
at the start, you've used an octal literal[^].
- In the first program, you output the number in decimal[^] (base 10) - the result is
8
.- In the second program, you output the number in octal[^] (base 8) - the result is
10
, since octal can only use digits0
to7
.- In the third program, you output the number in hexadecimal[^] (base 16) - the result is
8
.
这篇关于为什么C中的输出不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文