当这样增加时,C中发生了什么? [英] what is happening in C when increment this way?

查看:67
本文介绍了当这样增加时,C中发生了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您运行此程序,它将产生非常意外的结果。可以

任何人解释这个anamaly的性质? (还有什么功能

调用[和库包含linux / windows]来执行''暂停'');


#include< stdio。 h>


// void pause(){unsigned long i = 0;而(ⅰ++ LT; 10000000); }


int main(){

浮动i;


for(i = 0; i <100 ; i + = 0.1)//注意

{//暂停();

printf("%d \ n",i);

}


printf(" \ n");

返回0;

}

解决方案



puzzlecracker写道:

如果你运行这个程序,它会给意外的结果。谁可以解释这个anamaly的性质? (还有什么是函数
调用[和包含linux / windows的库]来执行''pause'');

#include< stdio.h>

int main(){
浮动i;

for(i = 0; i< 100; i + = 0.1)//注意
{// pause() ;
printf("%d \ n",i);


使用%f而不是%d(%d仅用于十进制整数)。

你通过调用undefined得到奇怪的结果行为

(转换指定与相应参数不匹配)。 }

printf(" \ n");
返回0;

}




puzzlecracker写道:

如果你运行这个程序,它会给出非常意外的结果。谁可以解释这个anamaly的性质? (还有什么功能
调用[和库包含linux / windows]来执行''pause'');

printf("%d \ nn,i) ;




此行是问题所在。 printifng浮动的格式说明符

(和双打)是%f(或%e或%g)。


- 约翰




John Valko写道:

puzzlecracker写道:

如果你运行这个程序,它会给意外的结果。谁可以解释这个anamaly的性质? (还有什么是
函数调用[以及包含linux / windows的库]来执行''pause'');

printf("%d \ n",i) ;



这一行是问题所在。 printifng浮动的格式说明符
(和双打)是%f(或%e或%g)。

- 约翰



我实际上是%f%----它确实打印但它变得怪异

尝试使用j%f


If you run this program, it will give very unexpected results. Can
anyone explain the nature of this anamaly? (also what is the function
call [and library to include linux/windows] to execute ''pause'');

#include<stdio.h>

//void pause(){ unsigned long i=0; while(i++<10000000); }

int main(){
float i;

for(i=0;i<100;i+=0.1) // pay attention
{ // pause();
printf("%d\n",i);
}

printf("\n");
return 0;
}

解决方案


puzzlecracker wrote:

If you run this program, it will give very unexpected results. Can
anyone explain the nature of this anamaly? (also what is the function
call [and library to include linux/windows] to execute ''pause'');

#include<stdio.h>
int main(){
float i;

for(i=0;i<100;i+=0.1) // pay attention
{ // pause();
printf("%d\n",i);
Use %f instead of %d (%d is to be used only for decimal integers).
You''re getting weird results by invoking undefined behaviour
(conversion specified mismatch with corresponding argument). }

printf("\n");
return 0;
}




puzzlecracker wrote:

If you run this program, it will give very unexpected results. Can
anyone explain the nature of this anamaly? (also what is the function
call [and library to include linux/windows] to execute ''pause'');

printf("%d\n",i);



This line is the problem. The format specifier for printifng floats
(and doubles) is %f (or %e or %g).

--John



John Valko wrote:

puzzlecracker wrote:

If you run this program, it will give very unexpected results. Can
anyone explain the nature of this anamaly? (also what is the function call [and library to include linux/windows] to execute ''pause'');

printf("%d\n",i);



This line is the problem. The format specifier for printifng floats
(and doubles) is %f (or %e or %g).

--John


I mean %f actually ---- it does print but it gets weirder
try it withj %f


这篇关于当这样增加时,C中发生了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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