opening an .exe after using Mingw - c

I followed the instructions in this video:(See Docs)
What happened is that I created a very basic program in C, here is the code:
#include <stdio.h>
int main()
{
printf("Hey Buddy!\n");
return 0;
}
I compiled it using Mingw and an .exe file was created. Here the problem begins...
When simply opening the file in windows, a cmd window that says "Hey Buddy!" opens and closes immediately.
When trying to run the .exe file using the command line, the same thing happens, but the command line window then becomes stuck and it is impossible to close it - only shutting off the computer can do it.
Your help would be very appreciated, and I am sorry if I am doing something dumb and not realizing it:)

Your program is fine. The main declaration is wrong. It should be
int main(void)
but the declaration in your question won't cause any problems. I'm just telling you this to set you off on the right path.
Of course when you double click on the executable, then a new console window appears and immediately disappears. The program prints a single line of text and returns immediately. That behaviour is as expected.
The problem with the console window that cannot be closed is not down to an error in your code, at least the code that is shown in the question cannot explain that. That is presumably an environmental problem with your machine and/or compiler installation. Or perhaps you just have not yet worked out how to close a console window.

Related

Cannot run C program from command prompt on windows 10

I am using Windows 10 OS. I installed MinGW for compiling C programs. I tried running my program using the gcc command on the Command Prompt. The file compiles and an executable file(.exe) is formed in the same folder as my source file. But when I try running this file, I keep getting the message 'Access is denied'. Also the .exe file vanishes after this. I do not know what is wrong. Please help me out.
P.S Another time I did the same thing mentioned above and the .exe file ran and I was able to see the output on the Command line. And this time the .exe file did not vanish either.
What was the command you put to compile the program? Also to compile and make an executable file, you have to put this command - "gcc -o nameofexecutablefile nameofsourcefile.c", to run this program just type the name of your executable file in cmd. And to stop the program's window from closing, put "system("pause");" right before the "return 0;" in your program, at the end of the program it will display "Press any key to continue..." and when you press any key, the window will close. Also check if you did any mistakes in your program. I'll also give an example -
------------CODE-----------
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Hello World\n");
system("pause"); // this will pause the window
return 0;
}
-------OUTPUT--------
Hello World
Press any key to continue...

A program in C language made by CodeBlocks IDE runs with a double-click in Windows but not in Ubuntu. why?

I am learning C. I am using Ubuntu as main OS but I also have Windows 7 for testing in another partition. I have made a program in C in both OSes using Code Blocks. When I double click the compiled file of program in windows it runs, but when I do the same in Ubuntu it does not run. I have also created .desktop file for it, but even then it doesn't run. But using the command.someone told me code GUI in.so how can i code GUI in it? also why it runs on windows?
./addition
makes it run in terminal. But I want to run it using GUI. I am clicking on its icon but its not opening.Do i need to code GUI for it?
source code is
#include <stdio.h>
int main(){
int a,s,d;
printf("type the values u want to add and give tab between them\n");
scanf("%d %d",&a,&s);
d=a+s;
printf("addition is %d",d);
system("read -p 'Press Enter to EXIT...' var");
return 0;
}
in linux if you run a shell program from the desktop, it does not have a std input or output attached. For this reason you cannot see anything about it.
A quick solution would be to open a terminal and run it from there.
Two easy options if you don't know how to run the program from terminal:
A) Drag the icon of the program into the terminal, it will automatically build the full path to run it
B) Move to the program's home folder and run it from there "./programName"
I hope this helps mate.

How to remove execution related text from output window of Code::Blocks

I am using Code::Blocks for programming in C. When I compile my program and execute it, the output window (i.e.. Windows Command prompt) displays some execution related text, these texts are not of use to me right now and dont want them to appear(see text below).
Hello, World!
Process returned 0 (0x0) execution time : 3.920 s
Press any key to continue.
I tried to change the settings in Code::Blocks but couldn't find any settings related to the output window and also I dont want the text "Press any key to continue" to appear. These texts appear only if I run the program through Code::Blocks and doesn't appear if I directly run the program.
Unfortunately, some things just cannot be changed, and that is one of them. There are some quirks used by some IDE's that just drive programmers crazy, but it can't be helped. There is a reason why it's there: the execution data can be used to find out whether the program worked properly (e.g. ended execution). You can use this data later when targeting execution time as one of the main focuses in coding the project. There may be other uses for it as you code more and more advanced projects.
It only appears when you execute your code from the compiler. It does not need getch() function to stop the screen.
But if you execute its .exe file directly, outside the compiler, you will notice that annoying message 'Process returned 0 (0x0) execution time : 3.920 s' doesn't show anymore. Moreover, you will need getch() function to stop the screen.
you may need to include stdio.h and then call getchar() before return 0
for example;
#include <iostream>
//add this library
#include <stdio.h>
using namespace std;
int main()
{
cout<<"I am a C++ programmer! "<<"Awesome!";
//add this line of code
getchar();
return 0;
}

C language - (compile and run but command (cmd.exe) does not appear

I am practising C but I am having problem with this.
I am using wxDev C++ and I ran the file (it says compiles and runs fine) but the problem is that the result does not appear at all (command prompt (cmd.exe) is not appearing).
I am currently using Windows 7 using MacBookPro (as bootcamp).
What can be the potential solutions toward this problem?
Thank you very much
e.g. even this simple code
int main(void){
printf("Hello, world");
getch();
}
The getch() function is waiting for you to press a key before it returns control to the program. The program will not finish executing until you type 1 character.
See http://www.programmingsimplified.com/c/conio.h/getch , which shows a similar program to yours.

How can I see an the output of my C programs using Dev-C++?

I'm looking to follow along with The C Programming Language (Second Addition) on a machine running Vista.
So far, I've found Dev-C++ the easiest IDE to do this in. However, I still have one problem. Whenever I run my compiled code, for example: a simple hello world program, it runs, but the console window just flickers on the screen, and I can't see the output.
How can I see an the output of my C programs using Dev-C++? I found a C++ specific solution, System("pause"), and a really ugly C solution, while looping fflush(stdout), but nothing nice and pretty.
I put a getchar() at the end of my programs as a simple "pause-method". Depending on your particular details, investigate getchar, getch, or getc
In Windows when a process terminates, the OS closes the associated window. This happens with all programs (and is generally desirable behaviour), but people never cease to be surprised when it happens to the ones they write themselves.
I am being slightly harsh perhaps; many IDE's execute the user's process in a shell as a child process, so that it does not own the window so it won't close when the process terminates. Although this would be trivial, Dev-C++ does not do that.
Be aware that when Dev-C++ was popular, this question appeard at least twice a day on Dev-C++'s own forum on Sourceforge. For that reason the forum has a "Read First" thread that provides a suggested solution amongst solutions to many other common problems. You should read it here.
Note that Dev-C++ is somewhat old and no longer actively maintained. It suffers most significantly from an almost unusable and very limited debugger integration. Traffic on the Dev-C++ forum has been dropping off since the release of VC++ 2005 Express, and is now down to a two or three posts a week rather than the 10 or so a day it had in 2005. All this suggest that you should consider an alternative tool IMO.
Use #include conio.h
Then add getch(); before return 0;
The easiest thing to do is to run your program directly instead of through the IDE. Open a command prompt (Start->Run->Cmd.exe->Enter), cd to the folder where your project is, and run the program from there. That way, when the program exits, the prompt window sticks around and you can read all of the output.
Alternatively, you can also re-direct standard output to a file, but that's probably not what you are going for here.
For Dev-C++, the bits you need to add are:-
At the Beginning
#include <stdlib.h>
And at the point you want it to stop - i.e. before at the end of the program, but before the final }
system("PAUSE");
It will then ask you to "Press any key to continue..."
Add this to your header file #include
and then in the end add this line : getch();
You can open a command prompt (Start -> Run -> cmd, use the cd command to change directories) and call your program from there, or add a getchar() call at the end of the program, which will wait until you press Enter. In Windows, you can also use system("pause"), which will display a "Press enter to continue..." (or something like that) message.
Add a line getchar(); or system("pause"); before your return 0; in main function.
It will work for you.
;
It works...
#include <iostream>
using namespace std;
int main ()
{
int x,y; // (Or whatever variable you want you can)
your required process syntax type here then;
cout << result
(or your required output result statement); use without space in getchar and other syntax.
getchar();
}
Now you can save your file with .cpp extension and use ctrl + f 9 to compile and then use ctrl + f 10 to execute the program.
It will show you the output window and it will not vanish with a second Until you click enter to close the output window.
i think you should link your project in console mode
just press Ctrl+h and in General tab select console.
When a program is not showing or displaying an output on the screen, using system("pause"); is the solution to it on a Windows profile.
The use of line system("PAUSE") will fix that problem and also include the pre processor directory #include<stdlib.h>.
Well when you are writing a c program and want the output log to stay instead of flickering away you only need to import the stdlib.h header file and type "system("PAUSE");" at the place you want the output screen to halt.Look at the example here.The following simple c program prints the product of 5 and 6 i.e 30 to the output window and halts the output window.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a,b,c;
a=5;b=6;
c=a*b;
printf("%d",c);
system("PAUSE");
return 0;
}
Hope this helped.

Resources