-2

When i type the command gcc filename.c a new file 'a.exe' is created, then i have to run a.exe to get my program to run.

Is there a way just to type one command to run my program or can you run a C program without having a new .exe file being created?

I use gcc version gcc (MinGW.org GCC-6.3.0-1) 6.3.0

(Complete beginner with C)

Evg
  • 25,259
  • 5
  • 41
  • 83
logan_9997
  • 574
  • 2
  • 14
  • 3
    `gcc filename.c && ./a.exe` – drescherjm Oct 29 '22 at 00:07
  • 2
    You never mention C++, so why the C++ tag? They are distinct languages. For example, C++ allows for function and operator overloading. C++ has string, vector and map data structures. – Thomas Matthews Oct 29 '22 at 00:07
  • @drescherjm is that the standard way of running C files? – logan_9997 Oct 29 '22 at 00:09
  • It's a way of combining the execution of 2 processes. `&&` executes the second program if the first one completed successfully. `&&` works for this in windows, linux and other systems. More on this here: [https://stackoverflow.com/questions/4510640/what-is-the-purpose-of-in-a-shell-command](https://stackoverflow.com/questions/4510640/what-is-the-purpose-of-in-a-shell-command) – drescherjm Oct 29 '22 at 00:14

2 Answers2

0

You can't run the .c file directly - you have to compile it into an executable (using gcc or whatever compiler). However, you don't have to compile it every time you want to run it - you only have to compile it after first creating it and after you make any changes. So you can run gcc once, then run a.exe multiple times.

You can name the executable something other than a.exe if you wish using the -o option:

gcc -o prog filename.c
./prog
John Bode
  • 119,563
  • 19
  • 122
  • 198
-1

You could make a .bat file that does everything for you. For example:

build.bat
  gcc %1.c -o %1.exe
  %1.exe

This will build and run the file for you:

build hello

will compile and run hello.exe for you.

user1234
  • 19
  • 3
  • You should never blindly run programs that compile with warnings. If you are going to try to automatically run a program after compilation, you should enable warnings (you should always enable warnings), but you should also enable `-Werror` so that compilation fails when there are warnings. [Here is an old answer of mine](https://stackoverflow.com/a/41861500/6879826) that talks about a solution I have used on Linux. – ad absurdum Nov 01 '22 at 17:43