-1

I wrote a simple program named b.c to call dir in Windows 10 cmd via C.

This is what the script looks like:

#include <stdlib.h>

int main()
{
    system("dir");
    return 0;
}

I typed this in cmd

gcc b.c
b

but it returns

J:\fundamental of C programming>b
'b' is not recognized as an internal or external command, operable program or batch file.

This is how I add path

enter image description here

I don't think it is path matter.

So how can I fix it?

phuclv
  • 37,963
  • 15
  • 156
  • 475
Gang
  • 85
  • 2
  • 9
  • 1
    Do dir in your current directory and find if there's any new executable created. By default, gcc will generate an executable like a.out or a.exe (in windows, but I am not so sure). – tibetty Aug 10 '17 at 03:45
  • Yes. Now I understand. I didn't specify the output file. mingw produces a.exe by default. But I run b instead. – Gang Aug 10 '17 at 04:01
  • 1
    The problem is that you can't find the executable to run — not that when the executable is run it cannot find `dir` to run. – Jonathan Leffler Aug 10 '17 at 05:27
  • "Fundamental of C Programming" *starts* with `system`? – Ajay Brahmakshatriya Aug 10 '17 at 05:55

3 Answers3

5

You need to run

gcc b.c -o b.exe

Without -o option it'll use the default output executable name which is a.exe on Windows and a.out on *nix systems. You can easily see the a.exe with the dir command

-o file

  • Place output in file file. This applies to whatever sort of output is being produced, whether it be an executable file, an object file, an assembler file or preprocessed C code.

  • If -o is not specified, the default is to put an executable file in a.out, the object file for source.suffix in source.o, its assembler file in source.s, a precompiled header file in source.suffix.gch, and all preprocessed C source on standard output.

https://gcc.gnu.org/onlinedocs/gcc/Overall-Options.html

For more information read Determining C executable name

Of course you can also run a instead of b

Community
  • 1
  • 1
phuclv
  • 37,963
  • 15
  • 156
  • 475
  • Thanks a lot. It works. But I don't understand why it produces an a.exe under current directory instead of b.exe – Gang Aug 10 '17 at 03:50
  • @Gang mingw produces a.exe on windows, if you are using it – Pras Aug 10 '17 at 03:54
  • @Gang the default output filename for gcc is `a` plus an extension regardless of the source file name – M.M Aug 10 '17 at 04:31
0

Compile like this

gcc -o b b.c

by specifying output filename

-o <output_file>
not_python
  • 904
  • 6
  • 13
0

Try to update bin path variable in windows open next link to see how: http://www.c4learn.com/c-programming/run-c-program-command-prompt-windows/