One easy way to do this is to get a standalone build of MinGW-w64 for Windows from https://winlibs.com/.
This will provide you with all the tools you need to compile and link Windows programs.
The command will be gcc instead of cc, but to run it, you should specify its complete path. When the download is extracted you can find it under mingw64\bin\gcc.exe. Or you can add it to the PATH environment variable (but I don't recommend this).
There are examples on how to use it at https://winlibs.com/.
If you are a beginner it is recommended to use a GUI like VSCode or Code::Blocks, so you won't need command line actions to compile and link your code, and you can even debug your code if you need to.
Answer from Brecht Sanders on Stack Overflowassembly - How to compile C programs using cc command in cmd - Stack Overflow
Compiling C-code from the Command Prompt in Windows? - Stack Overflow
windows - How to compile C program on command line using MinGW? - Stack Overflow
Running c program from command line in one step? - Stack Overflow
Videos
You do this:
Copycl app.c
Here's a complete transcript, including setting up the environment for Visual Studio 2005 (change "8" to "9.0" for Visual Studio 2008).
CopyC:\src\tests>"\Program Files (x86)\Microsoft Visual Studio 8\vc\bin\vcvars32.bat"
Setting environment for using Microsoft Visual Studio 2005 x86 tools.
C:\src\tests>type app.c
#include <stdio.h>
int main(void)
{
printf("Hello world!\n");
return 0;
}
C:\src\tests>cl app.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
app.c
Microsoft (R) Incremental Linker Version 8.00.50727.762
Copyright (C) Microsoft Corporation. All rights reserved.
/out:app.exe
app.obj
C:\src\tests>app
Hello world!
MinGW provides a popular command-line GCC compiler for Windows.
It indicates it couldn't find gcc.exe.
I have a path environment variable set to where MinGW is installed
Maybe you haven't set the path correctly?
echo %path%
does that include the path to gcc.exe? Otherwise, compilation is similar to Unix:
gcc filename.c -o filename
I've had this problem and couldn't find why it kept happening. The reason is simple: Once you have set up the environment paths, you have to close the CMD window, and open it again for it be aware of new environment paths.
You could do that with a makefile. More about GNU Make here.
Copyall:
gcc test.c -o test
./test
The file should be called Makefile or makefile (it can have different names,just keeping it simple), and you can run it by executing:
make
Assuming you have GNU Make installed and test.c is located in the same directory with makefile.
This is a bit tedious in the long run. Is there any way I could do this process in one step?
Yes. You could create a shell function (or an alias if your shell supports alias arguments, which bash does not), e.g.:
Copyccr() { gcc "$1" -o x.$$ && ./x.$$; rm -f x.$$ }
$ ccr hello.c
Hello, world!
$
which will compile the script, run it if compilation succeeded, then remove the compiled binary.
And perhaps also without creating the execuable?
No (well, not easily). Executing binaries is offloaded to the exec*() function family, and the operations performed are complex and, I suspect, incompatible with stdin operations. So you cannot send the executable to a pipe and execute it from the pipe.
What you can do is use a C interpreter, albeit it is not exactly the same thing.