My C program won't print anything even though I tried everything I read on Internet

Viewed 559

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.

2 Answers

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.

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

Related