Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 关于颜色的linux和windows_C - Fatal编程技术网

C 关于颜色的linux和windows

C 关于颜色的linux和windows,c,C,最近,我遇到了一个问题 #include <stdio.h> #include <stdlib.h> void main() { system("color 1F"); } #包括 #包括 void main() { 系统(“颜色1F”); } 这可以在Windows中打印,但不能在Linux中打印。为什么? 与c无关,您正在对不一定存在的命令执行系统调用 color存在于Windows shell中,但在Linux上不存在。您的代码在Linux上无法移植 Lin

最近,我遇到了一个问题

#include <stdio.h>
#include <stdlib.h>
void main()
{
  system("color 1F");
}
#包括
#包括
void main()
{
系统(“颜色1F”);
}
这可以在Windows中打印,但不能在Linux中打印。为什么?


与c无关,您正在对不一定存在的命令执行
系统
调用

color
存在于Windows shell中,但在Linux上不存在。您的代码在Linux上无法移植

Linux有自己的方法。您应该检查正在运行的操作系统并调用,例如,如果您检测到Linux(或在编译时),那么您已经涵盖了Windows和Linux


作为一种可移植的替代方案,标准在许多操作系统上也广泛可用(尽管适用于Windows)

void main()
很古老,您应该阅读另一本书
system()
执行命令
color 1F
是一个Windows命令。基本上,您的C代码执行Windows命令,您想知道为什么它在其他系统中不起作用。根据Jens a.Koch的说法(包括其他Windows版本的解决方法/建议),现在即使是Windows 10也支持ANSI颜色代码,您可能会改为推荐ANSI颜色代码?@NominalAnimal好的,但我认为转义序列需要一些调整才能在Windows上工作。例如,在Python中,我们必须使用
colorama
.True重定向输出;例如,Win 10,Threshold update 2之前的Windows用户确实需要该命令,或者使用Jens A.Koch的答案中列出的另一个命令提示。我只是建议这样做,因为毕竟ANSI颜色代码是最可移植的解决方案。它们往往在旧的Unix系统中得到支持。它们在所有POSIXy系统中都受支持——Linux、BSDs、Mac。MS-DOS过去通过ANSI.SYS支持ANSI转义码,但在某些情况下,Microsoft从Windows命令提示符中删除了该支持。它似乎是在2015年11月的阈值2更新中为Windows 10重新引入的。。有趣,阿米高斯支持它,没有人使用它。。。我猜微软已经开始意识到,如果不更加兼容Linux(现在有内置Windows10的UbuntuBash解释器),它就无法继续。这是最好的!