2

My Hello World program doesn't give any output. I have been struggling for a week trying to do different things mentioned on internet such as:

  • Adding \n at the end (as printf("Hello World\n");)
  • fflush(stdio);
  • getch();
  • I tried reinstalling C compiler
  • I went from ming64 to ming32
  • I even tried compiling it with g++
  • I tried it out on VS code too using command prompt

But no matter what, still I won't get output. The code is as follow:

#include <stdio.h>
int main(void) {
  printf("Hello World\n");
  return 0;
}

The output on console I get is as follow:

[Running] cd "g:\C WorkSpace\" && gcc Test.c -o Test && "g:\C WorkSpace\"Test
 
[Done] exited with code=0 in 0.453 seconds

The code does compile and creates an .exe file.
The executable file runs and doesn't print anything.

Edit: I have tried it on comamand prompt too but still it won't give output This is what it looks like

 Directory of G:\C WorkSpace

02-10-2021  06.34 PM    <DIR>          .
02-10-2021  06.34 PM    <DIR>          ..
02-10-2021  06.28 PM    <DIR>          .vscode
21-09-2021  08.22 AM                82 Hello World.c
02-10-2021  06.33 PM                83 Test.c
02-10-2021  06.34 PM            40,764 Test.exe
21-09-2021  09.57 AM    <DIR>          TestProjects
               3 File(s)         40,929 bytes
               4 Dir(s)  510,360,821,760 bytes free

G:\C WorkSpace>gcc test.c -o Test

G:\C WorkSpace>.\Test

G:\C WorkSpace>g++ Test.c -o Test

G:\C WorkSpace>.\Test

G:\C WorkSpace>

Edit 2: It has been pointed out that there must be an issue with my toolchain so I am updating the question with how I have installed it.

I downloaded the setup from https://sourceforge.net/projects/mingw/files/OldFiles/ downloaded the latest version of mingw-get-setup.exe(86.5kB)

I ran the setup it downloaded things, after that it took me to MinGW Installation Manager

I checked mingw32-base, mingw32-gcc-g++,mingw32-gcc-objc and updated catalog I am assuming it meant installation because it downloaded some files.

After that I setup the environment PATH as C:\MinGW\bin

Edit 4: I have tried running it in command prompt in all possible ways but it won't work

G:\C WorkSpace>gcc Test.c -o Test

G:\C WorkSpace>Test.exe

G:\C WorkSpace>.\Test.exe

G:\C WorkSpace>./Test.exe
'.' is not recognized as an internal or external command,
operable program or batch file.

G:\C WorkSpace>

Edit 5: I tried making a new c program(in VS code) and run it when I ran it it gave me output but when I run it again it doesn't give any output I tried that in cmd and it didn't give output. After that I tried making the file out of VS just in cmd and all the files which were not compiled in VS code are working correctly. Then I tried re-compiling the files which were compiled by VS code they still don't seem to work I think the issue is with VS code not the compiler or PATH variable. It will be really helpful if I was able to fix the issue.

Edit 6: Apparently there is some kind of issue with code-runner extension. When I compile the code without using code runner extension it works just fine. I found the cause of the problem, Please consider this thread as closed. Thank you everyone for the efforts.

Nav12
  • 51
  • 7
  • There is nothing wrong with the program. There is something wrong with your environment but it is hard to tell what exactly. What happens when you do ".\test.exe" in the console? – n. m. could be an AI Oct 02 '21 at 13:08
  • It is Windows. simply type `test` and press enter. `./test.exe` is Linux – 0___________ Oct 02 '21 at 13:17
  • Make sure you are building for the console subsystem, not the GUI subsystem. Try compiling with `-mconsole` or `-Wl,--subsystem,console`. – user3840170 Oct 02 '21 at 13:18
  • 2
    Just a minor hint on the side: Avoid path names with spaces in them, they are a major pain in the butt in all text based environment and should never have been allowed. – Peter - Reinstate Monica Oct 02 '21 at 13:25
  • 2
    Your program builds and runs [just fine](https://godbolt.org/z/z3xM5W1Ea), in itself. This looks like some sort of a toolchain problem. Can you add to your question exactly how you've installed mingw and gcc? Also, this question has now gotten interesting enough to merit an upvote :-P – einpoklum Oct 02 '21 at 13:25
  • @0___________ Interesting. I wanted to contradict because modern Windows generally accepts path names with (forward) slashes instead of backward slashes as well; for example, in the OP's case you could write `"G:/C WorkSpace/Test.exe"`. But apparently not just `./...`. – Peter - Reinstate Monica Oct 02 '21 at 13:30
  • Thanks for your perseverance! You can answer your own question, and accept it — this might make it easier for later readers to find your answer. – Steve Summit Jan 25 '22 at 13:15
  • 1
    I wonder if your problem with coderunner had anything to do with [this issue](https://stackoverflow.com/questions/36964949)? – Steve Summit Jan 25 '22 at 13:15

2 Answers2

0

Please go to the setting of VS code; Click on the extension settings on the left side; Search there terminal; Now find the option "run on terminal" turn it on.

-1

In windows operating system, when file compiled then its converted into executable format (.exe)

G:\C WorkSpace>gcc test.c -o Test  //to compile

G:\C WorkSpace>Test.exe  //to execute

you just have to change .\Test to Test.exe

Saurabh Dhage
  • 1,478
  • 5
  • 17
  • 31