1

I've been learning programming with C and have made some starter programs using CodeBlocks. The problem is that I can't run the application files that are saved in bin\debug by double right clicking on them. Instead, I have to open the project file and hit "Run" or "Build and Run" to do so. Is there a way to fix this?

Ardent Coder
  • 3,777
  • 9
  • 27
  • 53

1 Answers1

0

Firstly, "double click" means "double (left) click" and not "double (right) click".

Even if your machine does support "double right click" to open a file, the following could be a problem.

Consider a small program like this:

#include <stdio.h>

int main()
{
    printf("Hello World");
    return 0;
}

You can see its results when you run it inside the IDE because it waits for you to "press any key" before terminating it.

But when you run it outside the IDE, the program is terminated immediately at the end of main. The program will open, execute, and terminate before even you realizing it. It happens very quickly. In case of programs dealing with user inputs, you can see the intermediate results though.

Solution:

An easy fix is to add getchar() to the end of main. It will wait for you to press the enter key in order to quit the console application.

#include <stdio.h>

int main()
{
    printf("Hello World");
    getchar(); // <-- add this line of code
    return 0;
}

You can also have a more controlled termination by making your own exit function.

For example, if your program leaves a newline in the buffer before calling the getchar, it would consume the '\n' and not wait for you to press the enter key. In that case, you should clear the input buffer:

// clear the input buffer
char ch;
while ((ch = getchar()) != '\n' && ch != EOF);

// then call getchar
getchar();
Ardent Coder
  • 3,777
  • 9
  • 27
  • 53
  • I tried it on the "Hello world!" program and it worked. But when I tried it on a program that contains the scanf() function the result was the same. I'm not familiar with exit functions yet. – Βαγγέλης Κωστούλας May 16 '20 at 17:09
  • @ΒαγγέληςΚωστούλας So, basically your problem was that only. My program was just a demonstration; when you try it with your own programs, add `getchar()` like I've shown in the post. And don't worry about customized exit functions, that was just a better suggestion. – Ardent Coder May 16 '20 at 17:11
  • @ΒαγγέληςΚωστούλας Can you also elaborate on "the result was **the same**"? – Ardent Coder May 16 '20 at 17:33
  • It 's exactly what you described, the program is terminated immediately after I hit enter. With or without using the getchar() function you suggested, it doesn't make a difference when I use scanf(). – Βαγγέλης Κωστούλας May 16 '20 at 18:04
  • @ΒαγγέληςΚωστούλας I've extended my answer to meet this requirement. If you wonder what the problem in your code was, see [this article](https://www.geeksforgeeks.org/clearing-the-input-buffer-in-cc/). – Ardent Coder May 16 '20 at 20:14
  • I run the program from the article and again neither the string or the character where printed. I think I will try typing code on Notepad++ and complie it using cmd. Anyway, thanks for you time and effort. – Βαγγέλης Κωστούλας May 18 '20 at 12:39
  • @ΒαγγέληςΚωστούλας I didn't ask you to run the programs from that link, and neither did I check their programs. That was just to let you know about the buffer problem. See the last part of my answer where I took your scanf program into account as well. – Ardent Coder May 18 '20 at 12:41
  • I don't know if the is relevant, but this problem shows up when I'm working on Windows 10. When I type my code on Ubuntu, I use emacs and the gcc compiler to create the a.out file. Nothing wrong there. – Βαγγέλης Κωστούλας May 18 '20 at 22:51
  • Also, when I open the .exe file from cmd, everythings works fine. – Βαγγέλης Κωστούλας May 18 '20 at 22:56
  • @ΒαγγέληςΚωστούλας Relax, haven't you still copied the code in the last part of my answer and pasted it at the end of `main`? – Ardent Coder May 19 '20 at 10:35
  • 1
    Ι have and the problem is kinda solved. The thing is that I have to put these lines of code in my program every time. – Βαγγέλης Κωστούλας May 20 '20 at 15:19
  • @ΒαγγέληςΚωστούλας It depends on the environment where your program will be running. I mostly build console applications as an engine for a larger project, and a well defined API will take care of using the engine. You will not find the need to put those lines of code once you get out of the phase of building these starter programs that interacts with the user in a console window. All the best. – Ardent Coder May 20 '20 at 15:25