AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1489573
Accepted
Jinzu
Jinzu
Asked: 2019-10-06 18:11:23 +0800 CST2019-10-06 18:11:23 +0800 CST 2019-10-06 18:11:23 +0800 CST

为什么命令提示符和 PowerShell 给出的输出结果与终端不同?

  • 772

我有一本书的以下示例代码:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
  printf("Hello World (pid:%d)\n", (int) getpid());
  int rc = fork();

  if (rc < 0)
  {
    //fork failed
    fprintf(stderr, "fork failed\n");
    exit(1);
  }

  else if (rc == 0)
  {
    //child (new process)
    printf("Hello I am child (pid:%d)\n", (int) getpid());
  }

  else
  {
    //parent goes down this path (main)
    printf("Hello, I am parent of %d (pid:%d)\n",
            rc, (int) getpid());
  }

  return 0;
}

在 Windows(powershell 和命令提示符)中,我得到以下信息:

Hello World (pid:1283)
Hello, I am parent of 1284 (pid:1283)Hello I am child (pid:1284)

在 Linux(终端)中,我得到:

Hello World (pid:1923)
Hello, I am parent of 1924 (pid:1923)
Hello I am child (pid:1924)

如下图所示。

为什么相同的代码在不同的程序中显示不同?Windows 是否只是根据它读取代码的方式错误地显示输出?或者它的编译器是什么?我在 Linux 上使用包含的 gcc。我在 Windows 上使用 cygwin gcc。

Windows 上的 PowerShell

powershell-windows

Linux 上的终端

终端-linux

Windows 上的命令提示符

cmd窗口

还运行此代码:

#include <stdio.h>

int main()
{
    printf("Name\n");
    printf("Address Line 1\n");
    printf("Address Line 2");
}

在 Windows 上的 PowerShell 中正确打印代码。

在此处输入图像描述

linux powershell
  • 2 2 个回答
  • 339 Views

2 个回答

  • Voted
  1. Best Answer
    dave_thompson_085
    2019-10-06T20:39:46+08:002019-10-06T20:39:46+08:00

    对于 Cygwin 来说,显然存在一个竞争条件,它输出形成行主体的字符,然后是行尾 ( CRLF),并且另一个进程能够介于两者之间,因此您实际上得到body1 body2 CRLF CRLF了(为清楚起见,添加了间距)。

    标准 C 禁止“交互式设备”的标准输出被完全缓冲(即多行),但允许更少,并且无论如何操作系统(C 正式称为“主机环境”)如何处理来自多个进程的输出一旦“传输”到它完全超出了 C 的范围——如果我没记错的话,甚至在这种情况下是 POSIX——并不是说 Cygwin 无论如何都可以完全满足 POSIX。

    • 4
  2. Giacomo1968
    2019-10-06T18:21:24+08:002019-10-06T18:21:24+08:00

    您\n在 Windows 上应该使用 Unix 换行符 () \r\n。

    差异在这里得到了很好的解释:

    共有三种不同类型的换行符,它们最初都是主要操作系统独有的:Windows/DOS、Macintosh 和 Unix。使用 Mac 换行符的文档在 Windows 系统上看起来很糟糕,而在 Unix 上使用 Windows 换行符的文档也不会被正确解释。造成这种情况的原因是实际创建换行符的方式。默认情况下,Mac 使用单个回车符 ( <CR>),表示为\r. 另一方面,Unix 使用单个换行符 ( <LF>), \n. Windows 更进一步并使用两者,创建一个 () 组合,\r\n.

    要自己测试,只需更改以下几行:

    printf("Hello World (pid:%d)\n", (int) getpid());
    fprintf(stderr, "fork failed\n");
    printf("Hello I am child (pid:%d)\n", (int) getpid());
    printf("Hello, I am parent of %d (pid:%d)\n",
                rc, (int) getpid());
    

    对此:

    printf("Hello World (pid:%d)\r\n", (int) getpid());
    fprintf(stderr, "fork failed\r\n");
    printf("Hello I am child (pid:%d)\r\n", (int) getpid());
    printf("Hello, I am parent of %d (pid:%d)\r\n",
                rc, (int) getpid());
    

    如果希望跨平台与换行符保持一致,我建议您只需根据 OS/shell 版本将一个常量应用于您的代码,该常量将分配正确的换行符,然后将其分配给\n您代码中的所有位置.

    • 1

相关问题

  • 禁用后无法启用 Microsoft Print to PDF

  • 我可以让这个 PowerShell 脚本接受逗号吗?

  • 以 root 身份运行 docker 容器

  • 如何在域和 Linux 活动目录中启用指纹传感器

  • 如何在CentOS 7 中将Ctrl+C 永久更改为Ctrl+K?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    Windows 10 服务称为 AarSvc_70f961。它是什么,我该如何禁用它?

    • 2 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Marko Smith

    ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] 证书验证失败:无法获取本地颁发者证书 (_ssl.c:1056)

    • 4 个回答
  • Marko Smith

    我如何知道 Windows 安装在哪个驱动器上?

    • 6 个回答
  • Martin Hope
    Albin 支持结束后如何激活 WindowsXP? 2019-11-18 03:50:17 +0800 CST
  • Martin Hope
    fixer1234 “HTTPS Everywhere”仍然相关吗? 2019-10-27 18:06:25 +0800 CST
  • Martin Hope
    Kagaratsch Windows 10 删除大量小文件的速度非常慢。有什么办法可以加快速度吗? 2019-09-23 06:05:43 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    Inter Sys Ctrl+C 和 Ctrl+V 是如何工作的? 2019-05-15 02:51:21 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve