看看这段代码:
#include<stdio.h>
#include <unistd.h>
int main()
{
int pipefd[2],n;
char buf[100];
if(pipe(pipefd)<0)
printf("Pipe error");
printf("\nRead fd:%d write fd:%d\n",pipefd[0],pipefd[1]);
if(write(pipefd[1],"Hello Dude!\n",12)!=12)
printf("Write error");
if((n=read(pipefd[0],buf,sizeof(buf)))<=0)
printf("Read error");
write(1,buf,n);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我希望printf在从管道中读取Hello Dude之前打印Read fd并写入fd.但情况并非如此......请看这里.当我在我们的大学计算机实验室尝试相同的程序时,我的输出是
Read fd:3 write fd:4
Hello Dude!
Run Code Online (Sandbox Code Playgroud)
我们的朋友也很少观察到,更改printf语句以包含更多数量的\n字符会改变输出顺序......例如.. printf("\nRead fd:%d\n write fd:%d\n",pipefd[0],pipefd[1]);意味着打印了读取fd然后打印Hello Dude!了写入fd 的消息.这是什么行为?注意:Out lab使用我们运行终端的linux服务器,但我不记得编译器版本.
我在 Fedora 20 上使用 Anjuta 和 gdb 并创建了一个 C Makefile 项目。代码如下所示:
#include <stdio.h>
int main (void)
{
° printf ("1");
° printf ("2");
° printf ("3");
return (0);
}
Run Code Online (Sandbox Code Playgroud)
° 表示我在那个位置设置了一个断点。
现在,当我调试代码时,没有输出,而当前行是这些 printf 函数之一。只有当我退出 main '123' 时,终端中才会出现。
如果我将 \n 添加到第二个 printf 参数,那么当我从断点 2 移动到第三个断点时,输出会出现 '12'。
所以我很困惑.我正在阅读"从头开始编程"一书,并正在使用库.
printf工作得很好,只要我在字符串中包含一个"\n",但如果没有它,它将完全没有打印.
知道为什么会这样吗?
码:
.section .data
my_str:
.ascii "Jimmy Joe is %d years old!\n\0"
my_num:
.long 76
.section .text
.globl _start
_start:
pushl my_num
pushl $my_str
call printf
movl $1, %eax
movl $0, %ebx
int $0x80
此外,当我使用-m elf_i386进行32位模式和-dynamic-linker /lib/ld-linux.so.2 -lc进行链接时,我收到警告
ld:在搜索-lc时跳过不兼容的/usr/lib64/libc.so
如果这有任何区别,或者如果有人对如何直接加载32位库有任何建议.
谢谢!
我试图实现gecthar,使用read,问题是,当我使用my_getchar()与printf我的功能之前执行printf.
#ifndef BUFF_SIZE
#define BUFF_SIZE 1023
#endif
int my_getchar(void)
{
static char buff[BUFF_SIZE];
static char *chr;
int ret;
if ((ret = read(STDIN_FILENO, buff, BUFF_SIZE)) > 0)
{
chr = buff;
return (*chr);
}
return (EOF);
}
Run Code Online (Sandbox Code Playgroud)
在我的 main()
char c;
printf("Enter character: "); // if I put '\n' line here, it works fine
c = my_getchar();
printf("Character entered: ");
putchar(c);
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?
我想在同一个地方一个一个地打印一个字符序列。我打印一个字母,然后在睡眠状态下等待 1 秒钟,使用控制台代码将光标向左移动一列,打印下一个字母等等。问题是程序等待所有睡眠的总和(在我的示例中为 2s),然后只打印最后一个字符('y')。nanosleep 也是如此,等待信号而不是睡眠。如何使它工作?
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
printf( "H" );
sleep( 1 );
printf( "\033[1D" );
printf( "e" );
sleep( 1 );
printf( "\033[1D" );
printf( "y" );
}
Run Code Online (Sandbox Code Playgroud) 我有这个简单的代码"在int中的printinterval
sleep(printinterval/3);
displayPrint(); //// just some printing func
sleep(printinterval/3);
displayPrint();
sleep(printinterval-2*(int)(printinterval/3));
displayPrint();
Run Code Online (Sandbox Code Playgroud)
问题是它不能以正确的方式做延迟,跳过1次睡眠
但
sleep(printinterval/3);
printf("\n");
displayPrint(); //// just some printing func
printf("\n");
sleep(printinterval/3);
printf("\n");
displayPrint();
printf("\n");
sleep(printinterval-2*(int)(printinterval/3));
printf("\n");
displayPrint();
Run Code Online (Sandbox Code Playgroud)
奇迹般有效
有任何想法吗?谢谢 :)
编辑:thx的帮助发现这种方式来解决它
fflush(stdout); // Will now print everything in the stdout buffer
Run Code Online (Sandbox Code Playgroud)
thx
为什么我的程序不是 printf ?
先睡着然后写,但他应该反过来做。
#include <stdio.h>
#include <stdlib.h>
#ifdef _WIN32
#include <Windows.h>
#else
#include <unistd.h>
#endif
void sleepMilliSecond(long milliSecondInput) {
#ifdef _WIN32
Sleep(milliSecondInput); // v milliSecondach
#else
usleep(pollingDelay * 1000); //microsekundy -> milisekundy
#endif
}
int main(int argc, char** argv) {
printf("start sleep");
sleepMilliSecond(1000); //sleep 1s
printf("stop sleep");
return (EXIT_SUCCESS);
}
Run Code Online (Sandbox Code Playgroud)
程序的输出是:sleep 然后他写 start sleep stop sleep,为什么?
编辑: 工作解决方案是:
printf("start sleep");
fflush(stdout);
sleepMilliSecond(10000);
printf("stop sleep");
Run Code Online (Sandbox Code Playgroud) 在以下两个代码中,我无法理解这个问题.第一个代码是:
#include <stdio.h>
main() {
int num1, num2;
scanf("%d%d", &num1, &num2);
printf("I LOVE MY INDIA\n"); //here is '\n' after the statement
printf("%d", num1/num2);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这里,如果输入num1=2和num2=0然后在gcc编译的输出为:
我爱我的印度
浮点异常(核心倾倒)
但对于第二个代码:
#include <stdio.h>
main() {
int num1, num2;
scanf("%d%d", &num1, &num2);
printf("I LOVE MY INDIA"); //here is no '\n'
printf("%d", num1/num2);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
对于与之前相同的输入,显示:
浮点异常(核心转储)
在这两个代码之间只有一个区别.在第一个中有一个\n后面I LOVE MY INDIA和第二个代码中没有\n.请解释为什么I LOVE MY INDIA没有在第二个代码中显示.
int kr=0;
int ss =0;
while ((kr=getchar()) != EOF){
if(kr != '\n')
{
ss++;
}
printf("%d\n",ss);
}
Run Code Online (Sandbox Code Playgroud)
使用此代码,printf正在等待,直到我按下enter然后同时打印所有顺序ss值,就像这样
.有人可以解释这种行为吗?
#include <stdio.h>
int main(void) {
char str[50];
printf("Enter a string : ");
gets(str);
printf("You entered: %s", str);
return(0);
}
Run Code Online (Sandbox Code Playgroud)
我在 Eclipse 上运行代码,我希望得到以下结果:
Enter a string : abcde
You entered: abcde
Run Code Online (Sandbox Code Playgroud)
但是当我运行程序时,第一条打印语句没有出现在控制台中,并且在我输入任何字符串后,程序失败了。谁能告诉我为什么会这样?另外,你能告诉我如何修复我的代码以获得预期的结果吗?