2

I had a issue with running a c-program. I am using Turbo C++ compiler but when I write a code and compile there is no problem with compiling. But when I run program is doesn't display any output.

This is the code:

#include<stdio.h>
int main()
{
    printf("Hello World\n");
    return 0;
}
dbush
  • 205,898
  • 23
  • 218
  • 273
Bhumesh Polaswar
  • 23
  • 1
  • 1
  • 3

7 Answers7

6

let me solve your problem,buddy.

Problem: program doesn't display the output.

Reason:

Program execution takes milleseconds to display the output & turbo c++ compilor has not control over it. So, it's the responsibility of coder to control the execution to display the output.

Solutions:

1.using getch(); //it is predefined function of <conio.h>

2.using getchar(); //it is predefined function of <stdio.h>

Using getch();

Code:

 #include <stdio.h>
    #include <conio.h>
    
    int main()
    {
      printf("Hello world\n");
      getch();                  //it can be used to hold program execution,it wait until the user enters a character.
      return 0;
    } 

USING getchar();

Code:

    #include <stdio.h>
    int main()
    {
      printf("Hello world\n");
      getchar();                  //getchar() means get a character from user,if user press any key + enter then getchar() executes.
      return 0;
    } 


   
 
Community
  • 1
  • 1
Shivam Sharma
  • 1,015
  • 11
  • 19
  • Your comments about `getch` and `getchar` are incorrect. – Weather Vane May 17 '17 at 12:51
  • @WeatherVane why? both functions expects a character from console. then what is incorrect? can you tell me? – Shivam Sharma May 17 '17 at 12:54
  • The comments need to be reversed. `getch` does not behave as the comment says, `getchar` does as you say for `getch`. – Weather Vane May 17 '17 at 12:56
  • @BhumeshPolaswar mesion not buddy, you should to accept this answer by clicking tick mark button which is located in left side of this answer.. :) :) – Shivam Sharma May 18 '17 at 03:42
  • But which is the best software for c programming? – Bhumesh Polaswar May 18 '17 at 04:52
  • @BhumeshPolaswar GCC compiler is the most used and recommended compiler. For a beginner, I will suggest to start with Code::Blocks or Microsoft visual studio . Code Blocks is a very light weight IDE which is quite good to start with programming. :) – Shivam Sharma May 18 '17 at 08:48
  • @ShivamSharma which setup should I choose I find this on code block website codeblocks-16.01-setup.exe codeblocks-16.01-setup-nonadmin.exe codeblocks-16.01-nosetup.zip codeblocks-16.01mingw-setup.exe codeblocks-16.01mingw-nosetup.zip codeblocks-16.01mingw_fortran-setup.ex – Bhumesh Polaswar May 18 '17 at 08:54
2

I cannot comment, so I cannot ask for clarification and therefore I shall post an answer.

I think the problem is that the console window closes after the program execution ends. Try this:

#inlcude <stdio.h>

int main(int argc, char** argv)
{
   printf("Hello world\n");
   getchar();
   return 0;
}

The getchar() function waits for your input and stops the execution until you hit enter key.

2

Turbo C by default closes the screen immediately so you have to hold the screen by calling a function which is defined inside conio.h header file

#include <stdio.h>
#include <conio.h>

int main()
{
  printf("Hello world\n");
  getch();
  return 0;
} 

The getch() method waits for your input before closing the screen.

Though I would not recommend using Turbo C as it is outdated. You should start using something like gcc with Codeblocks or some other IDE.

iamsankalp89
  • 4,607
  • 2
  • 15
  • 36
codelyzer
  • 467
  • 1
  • 4
  • 17
1

In order to see your output,you must hold your console window. Here are some methods. Include the conio.h file to your program and add the function, getch() at the end of program above return statement. If u r working on windows, you may use the command system ("PAUSE"); at the end of program although this is a dos command and works in windows only but u can use it. However, portability will be a concern here.

Biswajit Roy
  • 508
  • 2
  • 7
  • 19
0

Simply use getch() which is predefined function of <conio.h>, then you will get the output screen

#include<stdio.h>
#include<conio.h>
int main()
{
    printf("Hello World\n");
    getch();
    return 0;
}
iamsankalp89
  • 4,607
  • 2
  • 15
  • 36
0

I encountered the exact same problem using Dev-C compiler, and I think I found the problem he had. This may serve well for someone at a later . The solution that I realized, was that it wasn't the program, nor the compiler. It was a simple over-sight on my behalf. I had previously been working on a Win32 Gui program. That's what sounds like to be the problem here. I think the problem is the hash tag "-mwindows" may be turned on in the compiler options settings. That tag tells the compiler not to make the unwanted console window when building the GUI program. I rewrote the code, and save to a file and compiled on the command-line.... gcc -o main main.c ... it compile with errors and ran perfect. Unselected the tag in compiler options and re-created a new project as a console project. Then all was back to normal. So just make sure whether the default GUI settings are turned off, or do not add -mwindows at the end of the command line syntax, and the Linker will know to create the console. The console window is not the same window as the Windows Cmd Prompt window. They look the same but are not, I think the program console window is basically a double-buffered Device Context that allows you to see your program in windows.

ken
  • 1
  • 1
    Correction of typo-> "with errors", -> "it compiled with NO Errors and ran perfectly. – ken May 07 '20 at 21:05
-1

It's not something with the code, to get output after compiling (Alt+C or Alt+F9), then run (Alt+R or Ctrl+F9) you have to press (Alt+W) then choose Output from the options.