-1

Recently, I've been having a problem.

#include <stdio.h>
#include <stdlib.h>
void main()
{
  system("color 1F");
}

This can be printed Windows, but not in the Linux. Why?

Error

Michał Walenciak
  • 4,257
  • 4
  • 33
  • 61
xiang yang
  • 11
  • 1

1 Answers1

6

Nothing to do with c, you're performing a system call on a command that doesn't necessarily exist.

color exists in the Windows shell, but doesn't on Linux. Your code is just not portable on Linux as-is.

Linux has its own way of doing it. You should check which OS you're running on and call the setterm instead for instance if you detect Linux (or at compilation time), so you already have Windows & Linux covered.

As a portable alternative, standard ANSI escape sequences are also widely available on a lot of OSes (For Windows, you need Windows 10, though)

Community
  • 1
  • 1
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • 1
    Perhaps recommend ANSI color codes instead, now that even Windows 10 supports them (again), according to Jens A. Koch's [answer here](http://stackoverflow.com/a/38617204/1475978) (including workarounds/suggestions for other Windows versions)? – Nominal Animal Mar 06 '17 at 13:22
  • @NominalAnimal okay but I think escape sequences need some tweaking to work on Windows. For instance in Python we have to redirect output using `colorama`. – Jean-François Fabre Mar 06 '17 at 13:24
  • True; Windows users prior to Win 10, Threshold update 2, do need that or use one of the alternative command prompts listed in that Jens A. Koch's answer, for example. I only suggested this, because ANSI color codes are the most portable solution, after all. They tend to be supported in old Unix systems. They are supported in all POSIXy systems -- Linux, BSDs, Macs. MS-DOS used to support ANSI escape codes via ANSI.SYS, but at some point, Microsoft removed the support from the Windows command prompt. It seems it was reintroduced for Windows 10 in Threshold 2 Update in November, 2015.. Funny. – Nominal Animal Mar 06 '17 at 13:58
  • AmigaOS supports it and noone uses it... I guess Microsoft is beginning to realize that it cannot continue without being more Linux compliant (with ubuntu bash interpreter built-in windows 10 now). That's for the best! – Jean-François Fabre Mar 06 '17 at 14:03