linux下的printf();
现将要输出的内容输出到,缓冲区中然后,在以下条件下,再由缓冲区输出到屏幕,
==================以上由网友提供==============================================================
在Linux下运行以下程序。
#include <stdio.h>
void main()
{
printf("1");
printf("2");
printf("3");
printf("4");
sleep(3);
printf("ok");
}
结果不是我们想像的,输出1234停止三秒然后输出ok。
运行情况是这样的,
将1234输送到缓冲区(由于没有达到从缓冲区输送到屏幕上的任意一项,因此不进行输出,等待,程序停止三秒,将ok存到缓冲区,程序结束(主进程结束),此时将缓冲区的内容输送至屏幕)
导致我们看到的是,三秒停滞,然后出现1234ok。
我们可以改变程序使他,达到刷新缓冲区的效果
以及fpirntf();
有和printf();类似的机制。因此写文件是要注意。
还有个小问题,printf("....",表达式1,表达式2,表达式3,...);
中的表达时自右向左执行的。
#include <stdio.h>
int main()
{
int a=0,b=0,c=0,d=0;
printf("%d %d %d%d\n",a+=b,b+=c,c+=d,d+=1);
}
以下是求助,也是上述fprintf();结论给出的原因。求大侠解释。
=============================================================================================
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
void* showy(void* argv);
void * showx(void * argv )
{
int x=3;
int y=3,z=3;
pthread_t showythread;
int status_2;
printf("x is:%d
y:%dz:%d\n",x,y,z);
//printf("main a:%d
x:%d\n",*((int *)argv),x);
pthread_create(&showythread,NULL,showy,(void*)(&x));
pthread_join(showythread,(void*)(&status_2));
//sleep(3);
//printf("%p %p%p\n",&x,&y,&z);
printf("\nx is:%d
y:%dz:%d\n",x,y,z);
printf("y deat case :%d\n",status_2);
printf("showx thread start!\n");
x=3;
{
}
void* showy(void* argv)
{
}
int
main()
{
FILE *fp;
pthread_t showxthread;
int a=2,b=2,c=2,ta,tb,tc;
int status;
{
//
}
文件中的数据让人安心多了,在产生新线程前后,该线程中的数据,不会因为,他所等待的线程空间释放,而改变。