在 C 中处理数字

发布于 2024-11-14 17:50:38 字数 555 浏览 3 评论 0原文

无法理解 C 中如何处理数字。任何人都可以指出一个好的教程。

#include<stdio.h>
main()
{
    printf("%f",16.0/3.0);
}

此代码给出:5.333333

#include<stdio.h>
main()
{
    printf("%d",16.0/3.0);
}

给出了一些垃圾值:1431655765

然后

#include<stdio.h>
main()
{
    int num;
    num=16.0/3.0;
    printf("%d",num);
}

给出:5

然后

#include<stdio.h>
main()
{
    float num;
    num=16/3;
    printf("%f",num);
}

给出:5.000000

Couldnt understand how numbers are handled in C. Could anyone point to a good tutorial.

#include<stdio.h>
main()
{
    printf("%f",16.0/3.0);
}

This code gave: 5.333333

But

#include<stdio.h>
main()
{
    printf("%d",16.0/3.0);
}

Gave some garbage value: 1431655765

Then

#include<stdio.h>
main()
{
    int num;
    num=16.0/3.0;
    printf("%d",num);
}

Gives: 5

Then

#include<stdio.h>
main()
{
    float num;
    num=16/3;
    printf("%f",num);
}

Gives: 5.000000

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

落叶缤纷 2024-11-21 17:50:38

printf 被声明为

int printf(const char *format, ...);

第一个参数(format)是字符串,其余的可以是任何东西。其余参数的使用方式取决于 format 中的格式说明符。如果:

printf("%d%c", x, y);

x 将被视为 int,y 将被视为 char
所以,

 printf("%f",16.0/3.0);

没关系,因为你要求 float double (%f),所以传递 float double(16.0/3.0)

printf("%d",16.0/3.0);

你要求 int(%d),你传递 < Strike>float double(double和int具有不同的内部表示)因此,16.0/3.0(double)的位表示对应于1431655765(国际)。

 int num;
 num=16.0/3.0;

编译器知道您正在分配给 int,并为您转换它。请注意,这与之前的情况不同。

printf is declared as

int printf(const char *format, ...);

the first arg (format) is string, and the rest can be anything. How the rest of the arguments will be used depending on the format specifiers in format. If you have:

printf("%d%c", x, y);

x will be treated as int, y will be treated as char.
So,

 printf("%f",16.0/3.0);

is ok, since you ask for float double (%f), pass float double(16.0/3.0)

printf("%d",16.0/3.0);

you ask for int(%d), you pass float double (double and int have different internal representation) so, the bit representation of 16.0/3.0 (double) corresponds to bit representation of 1431655765(int).

 int num;
 num=16.0/3.0;

compiler knows that you are assigning to int, and converts it for you. Note that this is different than the previous case.

生生漫 2024-11-21 17:50:38

好的,第一个 1 给出了预期的正确值。

第二个你传递一个浮点数,而它将其视为 int (因此“%d”用于显示 int 数据类型,解释原因有点复杂,因为它看起来你才刚刚开始,我不担心为什么“%d”在传递浮点数时会这样做)读取错误,因此给你一个奇怪的值。 (虽然不是垃圾值)。

第三个,它将 16.0/3.0 设为 int,同时将其分配给 int 数据类型,这将导致 5。因为在将 float 设为 int 时,无论四舍五入如何,它都会去除小数。

在第四个中,右侧 (16/3) 被视为 int,因为末尾没有 .0 零。它评估然后将 5 分配给 float num。从而解释输出。

Ok, the first 1 is giving correct value as expected.

Second one you are passing a float while it is treating it as an int (hence the "%d" which is for displaying int datatypes, it is a little complicated to explain why and since it appears your just starting I wouldn't worry about why "%d" does this when passed a float) reading it wrong therefore giving you a wierd value. (not a garbage value though).

Third one it makes 16.0/3.0 an int while assigning it to the int datatype which will result in 5. Because while making the float an int it strips the decimals regardless of rounding.

In the fourth the right hand side (16/3) is treated as an int because you don't have the .0 zero at the end. It evaluates that then assigns 5 to float num. Thus explaining the output.

最初的梦 2024-11-21 17:50:38

这是因为您选择的格式字符串与您传递的参数不匹配。我建议查看 printf 的文档。如果你有“%d”,它需要一个整数值,那么该值的存储方式是无关紧要的,并且可能取决于机器。如果你有一个“%f”,它需要一个浮点数,也可能取决于机器。如果您这样做:

printf( "%f", <<integer>> );

printf 过程将查找一个浮点数,其中您给出了一个整数,但它不知道它的值和整数,它只是查找适当的字节数并假设您有把正确的东西放在那里。

16.0/3.0 is a float
int num = 16.0/3.0 is a float converted to an int
16/3 is an int
float num = 16/3 is an int converted to a float

您可以在网络上搜索 printf 文档。其中一页位于 http://linux.die.net/man/3/printf

It is because the formatting strings you are choosing do not match the arguments you are passing. I suggest looking at the documentation on printf. If you have "%d" it expects an integer value, how that value is stored is irrelevant and likely machine dependent. If you have a "%f" it expects a floating point number, also likely machine dependent. If you do:

printf( "%f", <<integer>> );

the printf procedure will look for a floating point number where you have given an integer but it doesn't know its and integer it just looks for the appropriate number of bytes and assumes that you have put the correct things there.

16.0/3.0 is a float
int num = 16.0/3.0 is a float converted to an int
16/3 is an int
float num = 16/3 is an int converted to a float

You can search the web for printf documentation. One page is at http://linux.die.net/man/3/printf

撩发小公举 2024-11-21 17:50:38

您可以使用隐式类型转换的概念来理解 C 中的数字。

在计算任何表达式时,它都遵守非常严格的类型转换规则。
你的表达式的答案取决于这个类型转换规则。

如果操作数的类型不同,则在继续操作之前,“较低”类型会自动转换为“较高”类型。
结果是较高类型的。

1:
所有 shortchar 都会自动转换为 int,然后是

2:
如果其中一个操作数是int,另一个是float,则int会转换为float,因为float是高于 ** int**。

如果您想了解有关隐式转换的更多信息,您必须参考 E Balaguruusamy 的Programming in ANSI C一书。

谢谢。
再见:迪普

You can understand numbers in C by using concept of Implecit Type Conversion.

During Evaluation of any Expression it adheres to very strict rules of type Conversion.
and your answer of expression is depends on this type conversion rules.

If the oparands are of different types ,the 'lower' type is automatically converted into the 'higher' type before the operation proceeds.
the result is of the higher type.

1:
All short and char are automatically converted to int then

2:
if one of the operands is int and the other is float, the int is converted into float because float is higher than an ** int**.

if you want more information about inplicit conversion you have to refer the book Programming in ANSI C by E Balagurusamy.

Thanks.
Bye:DeeP

我最亲爱的 2024-11-21 17:50:38

printf 将一段内存格式化为人类可读的字符串。如果您指定内存位应被视为浮点数,您将获得浮点数的正确表示;但是,如果您指定内存位应被视为整数并且它是浮点数,那么您将得到垃圾。

printf formats a bit of memory into a human readable string. If you specify that the bit of memory should be considered a floating point number, you'll get the correct representation of a floating point number; however, if you specify that the bit of memory should be considered an integer and it is a floating point number, you'll get garbage.

许你一世情深 2024-11-21 17:50:38
printf("%d",16.0/3.0);

16.0/3.0 的结果是 5.333333,以 单精度浮点格式表示如下

0 | 10101010 | 10101010101010101010101

如果将其读取为 32 位整数值,则结果将为 1431655765


num=16.0/3.0;

相当于num = (int)(16.0/3.0)。这会将浮点值 (5.33333) 的结果转换为整数 (5)。


printf("%f",num);

与 printf("%f",(float)num); 相同

printf("%d",16.0/3.0);

The result of 16.0/3.0 is 5.333333 which is represented in Single precision floating-point format as follows

0 | 10101010 | 10101010101010101010101

If you read it as 32bit integer value, the result would be 1431655765.


num=16.0/3.0;

is equivalent to num = (int)(16.0/3.0). This converts the result of float value(5.33333) to integer(5).


printf("%f",num);

is same as printf("%f",(float)num);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文