Why do many people believe that programs written in the C language can only run in a command prompt window?

PainterEngine Console Translation

This is a console for PainterEngine, the code is as follows:

Running it looks like this:

Nothing special, right? A small issue, this console can have buttons added and can change the background color.

Of course, that’s not all. It can also display images.

That’s not all; it even supports GIF animations. You could even use it as a chat box.

But even that’s not all, because it can even play videos.

What’s wrong with such an awesome console, a plain black window?

Don’t be too biased, who says you can only run in a black window? There are blue ones (PowerShell, of course, you can make it colorful if you want), white ones (SSH software), and gray ones (VSCode)…

Because they haven’t seen windows of other colors, that’s all.

What’s so strange about it?

On the Linux platform, there is a well-known graphical user interface library called GTK+. It has been primarily supported in the C programming language from the beginning.

WinMain Function (winbase.h) - Win32 apps

Hello World

You need to download a Windows SDK to compile the above code.

{Windows SDK - Windows Application Development}

Why are there louvered windows? Isn’t the monitor all black?

C Language itself doesn’t even have a black window.

The black window is called a “terminal emulator,” which is another piece of software unrelated to the C language. The content you type on the keyboard is passed to your program by it and is eventually obtained by your scanf. The content you printf is displayed on the screen with its help.

When you write your own program, it is nearly impossible to display things directly on the screen. The operating system manages all hardware, and you can only “ask it for help” to output things through a series of interfaces encapsulated by the operating system, drivers, and other software layers. These things include text, images, videos, sounds, and more.

The simplest way among them is to send something to the standard output (e.g., using printf), and then the terminal emulator helps you display it on the screen. If you need to display other things or interact with the user in other ways (keyboard, mouse, game controller, microphone, etc.), you need to call the interfaces provided by the operating system or more convenient interfaces built on top of it. This involves installing and using other libraries.

Of course, these libraries are very likely written in the C language. But instead of writing them yourself, you can directly use libraries that others have already written.

Not exactly, but introductory C language education typically doesn’t teach how to create graphical user interfaces because it’s quite cumbersome to do so using pure C.

Windows has two types of processes. Processes with windows have a built-in FIFO message queue, and the operating system sends messages to it.

To display a window, you need to call functions from user32.dll, and you also need to initialize some parameters, which can be a bit cumbersome.

Running in a Window is Possible, for Example:

Compile command:

Running result:


In addition, I have written a GUI program in pure C language, which is 18KB in size. Its function is to convert text from the clipboard into QR codes using the Ctrl-Alt-Q hotkey and hide with another press. Text longer than a certain length is automatically converted into multiple QR codes, and you can navigate through them with left/right clicks. This program can be used to copy text to your phone by scanning QR codes.

If written in C++, it would be over 200KB.

If you’re interested, you can download it here:

{Releases · znsoooo/qr-desktop (github.com)}

Indeed, that’s how humble C language is.

Graphical programming can be said to be one of the areas where C language is not particularly adept. Most C language graphics libraries are more like toys and are inadequate for larger projects.

Let’s touch on one point: C language lacks native inheritance. Those who have worked with graphical programming will understand that creating a so-called client involves nesting large frames within smaller frames, assembling different components like building blocks to create fancy interfaces.

Using object-oriented thinking, it becomes much more natural: large components can contain smaller components, and small elements can directly reuse the styles of large components…

Anyway, it’s the 21st century, and I can hardly believe that anyone is still using Win32 API to write clients (even MFC is almost extinct).

Because it was too early, there was no concept of a graphical interface when it was born.

At that time, computers only appeared in universities and advanced laboratories and were used by a few elite individuals. So, they only used black window command lines.

Graphical interfaces emerged later when the Silicon Valley crowd brought computers out of the laboratories for commercial use.

With commercial use, there was a need to cater to non-professionals. The requirement for graphical interfaces, which didn’t require memorizing commands, arose.

In general, pure C-based programs like VI, VIM had some graphical user interface (including BIOS and TC UI, which also appeared during that time).

We know that the earliest “Xianjian” (Chinese RPG game) was the DOS version, which was written in C and used the SDL engine, which was also quite cumbersome.

In essence, C isn’t limited to running only in a black window; it’s just that implementing a graphical interface was too complex. That’s why, later on, Windows introduced the C++ SDK and MFC.

Of course, nowadays both of these approaches are considered cumbersome, and they are gradually being abandoned. So, at present, it’s extremely rare to see graphical interfaces written purely in C. You can still catch a glimpse of them in programs like VI and VIM that have retained some traces of the past.

You can explore the SDL library, which allows your C language programs to break free from the black window.

On Windows, please study “Windows Programming.” This book will teach you how to interact with the operating system.

Black window ✗

Window without color codes √

Clearly, it’s because they don’t know how. Many people start learning C language by running it in the command prompt, and they might not continue to learn further. There might also be fewer people who use C to write graphical programs, so many assume it can only run in a command prompt.

Even though I don’t know it either, I believe it should be possible to create graphical interfaces. After all, OpenGL, for instance, seems to be based on C.

Back then, it needed a system terminal to ensure it had a user interface for its standard output and input.

If you write a GUI program in C language, it won’t open a terminal because all interactions with the user happen within the GUI, not in a terminal. In this case, you also need to ensure that you don’t use standard input and output because the user won’t see them and can’t respond to them.

Recommend following good traditions

** Ask if it is, then ask why**

It’s not that you can’t change to other colors.

It’s just that black is more familiar.

Next
Previous