在 c 中为浮点变量生成整数输出
To generate an integer output for a float variable in c
好的,这实际上是一个非常简单的代码,但由于我才刚刚开始学习 C,请耐心等待并帮助我。我会将我的问题作为注释放在代码旁边,以便很容易将我对代码的哪一部分有疑问联系起来。
#include <stdio.h>
main()
{
int first_no, second_no;
float dec_no, output_no;
first_no = 75;
second_no = first_no/2;
dec_no = 35.3;
output_no = dec_no/3;
printf("First No:%d\n", first_no);
printf("Second No:%d\n", second_no);
printf("Third No:%d\n",output_no);
/*here I wanted to print only the integer part of the output_no */
}
问题是我有一本书,它显示第三个编号的值为 0。
然后在另一个程序中显示编译时错误。
第二个节目:
#include <stdio.h>
void main()
{
int x = 5.3%2;
printf("Value of x is %d", x);
}
对于这个程序,书上说会出现编译时错误。我不明白为什么会这样。根据我的说法,输出应该是 1。
如果我使用下面的代码代替之前的代码:
#include <stdio.h>
main()
{
int first_no, second_no;
float dec_no, output_no;
first_no = 75;
second_no = first_no/2;
dec_no = 35.3;
output_no = dec_no/3;
printf("First No:%d\n", first_no);
printf("Second No:%d\n", second_no);
printf("Third No:%d\n",dec_no);
}
我应该期待什么输出?我仍然得到零或一些不可预测的输出吗?
使用 just
的问题
printf("Third No:%d\n",output_no);
是:
output_no
在传递给 printf
之前被转换为 double
。
- 当
printf
将 %d
视为格式说明符时,它需要一个 int
。当传递的对象是 double
类型时,行为未定义。
如果要打印浮点数的截断整数值,可以执行以下操作之一。
创建一个整数类型的临时变量并赋给浮点数
int temp = output_no;
printf("Third No:%d\n", temp);
将浮点数显式转换为整数类型。
printf("Third No:%d\n", (int)output_no);
printf("Third No:%d\n",dec_no);
What output should I expect? Do I
still get a zero or some unpredictable output?
As of the printf
function is concerned,
当您尝试使用用于 float(或)double 的格式说明符打印整数值时,行为是不可预测的,反之亦然。
但是可以使用 %c 来打印整数值的等效字符。也可以使用 %d 打印字符的 ASCII 值(整数表示)。
Second program: For this program, the book says that a compile time
error will be shown.
7.3.3 expression % expression
The binary % operator yields the remainder from the division of the first expression by the second. Both operands must be int or char, and
the result is int. In the current implementation, the remainder has
the same sign as the dividend.
在您的例子中,您提供了一个值 5.3
,因此它既不是 char 也不是 int,这就是它产生编译错误的原因。
如果您仍想 运行 该程序,您可以使用 fmod() 函数来实现。
试试这个代码:
#include<stdio.h>
#include<math.h>
void main()
{
float x=5.3;
int c =2;
printf("Value of xremainer is %lf",fmod(x,c));
}
编译为:
$gcc test.c -lm
好的,这实际上是一个非常简单的代码,但由于我才刚刚开始学习 C,请耐心等待并帮助我。我会将我的问题作为注释放在代码旁边,以便很容易将我对代码的哪一部分有疑问联系起来。
#include <stdio.h>
main()
{
int first_no, second_no;
float dec_no, output_no;
first_no = 75;
second_no = first_no/2;
dec_no = 35.3;
output_no = dec_no/3;
printf("First No:%d\n", first_no);
printf("Second No:%d\n", second_no);
printf("Third No:%d\n",output_no);
/*here I wanted to print only the integer part of the output_no */
}
问题是我有一本书,它显示第三个编号的值为 0。
然后在另一个程序中显示编译时错误。
第二个节目:
#include <stdio.h>
void main()
{
int x = 5.3%2;
printf("Value of x is %d", x);
}
对于这个程序,书上说会出现编译时错误。我不明白为什么会这样。根据我的说法,输出应该是 1。
如果我使用下面的代码代替之前的代码:
#include <stdio.h>
main()
{
int first_no, second_no;
float dec_no, output_no;
first_no = 75;
second_no = first_no/2;
dec_no = 35.3;
output_no = dec_no/3;
printf("First No:%d\n", first_no);
printf("Second No:%d\n", second_no);
printf("Third No:%d\n",dec_no);
}
我应该期待什么输出?我仍然得到零或一些不可预测的输出吗?
使用 just
的问题printf("Third No:%d\n",output_no);
是:
output_no
在传递给printf
之前被转换为double
。- 当
printf
将%d
视为格式说明符时,它需要一个int
。当传递的对象是double
类型时,行为未定义。
如果要打印浮点数的截断整数值,可以执行以下操作之一。
创建一个整数类型的临时变量并赋给浮点数
int temp = output_no; printf("Third No:%d\n", temp);
将浮点数显式转换为整数类型。
printf("Third No:%d\n", (int)output_no);
printf("Third No:%d\n",dec_no);
What output should I expect? Do I still get a zero or some unpredictable output? As of the
printf
function is concerned,
当您尝试使用用于 float(或)double 的格式说明符打印整数值时,行为是不可预测的,反之亦然。 但是可以使用 %c 来打印整数值的等效字符。也可以使用 %d 打印字符的 ASCII 值(整数表示)。
Second program: For this program, the book says that a compile time error will be shown.
7.3.3 expression % expression
The binary % operator yields the remainder from the division of the first expression by the second. Both operands must be int or char, and the result is int. In the current implementation, the remainder has the same sign as the dividend.
在您的例子中,您提供了一个值 5.3
,因此它既不是 char 也不是 int,这就是它产生编译错误的原因。
如果您仍想 运行 该程序,您可以使用 fmod() 函数来实现。
试试这个代码:
#include<stdio.h>
#include<math.h>
void main()
{
float x=5.3;
int c =2;
printf("Value of xremainer is %lf",fmod(x,c));
}
编译为:
$gcc test.c -lm