为什么C中的输出不同? [英] Why are the outputs different in C?

查看:94
本文介绍了为什么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 value 010 which is octal for the decimal value 8.
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 ,您已经使用了八进制文字 [ ^ ]。



  • 在第一个程序中,您输出的数字为十进制 [ ^ ](基数10) - 结果是 8
  • 在第二个程序中,输出八进制 [ ^ ](基础8) - 结果是 10 ,因为八进制只能使用数字 0 7
  • 在第三个程序中,您输出十六进制中的数字[ ^ ](基数为16) - 结果为 8
In all three programs, the variable a contains the number 8. By putting 0 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 digits 0 to 7.
  • In the third program, you output the number in hexadecimal[^] (base 16) - the result is 8.


这篇关于为什么C中的输出不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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