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

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.

Related

Code wont compile or throw an error when using scanf in C, gets stuck building forever

I want to preface this with the information that I am pretty inexperience with coding.
Whenever I try to compile my code, it never finishes building and never throws an error. I then have to use Task Manager to stop stuck.exe (stuck is the name of the c file) so that I can try to compile again. I have narrowed down the issue to having something to do with the scanf function.
#include <stdio.h>
int main(void) {
int number = 0;
printf("this line shouldn't break anything. number = %d\n", number);
printf("what should the new value of number be?: ");
scanf("%d", &number);
return 0;
}
When I remove the line that has the scanf function, the rest of the code compiles as it should.
I am doing all of this in SublimeText3 on Windows 10 and using GCC provided by MinGW.
Any information you can give to help me would be appreciated, and If you would like any more information please let me know.
If you have a process stuck.exe, it means that the program finished compiling and was automatically started by the IDE/text editor. scanf reads from standard input, but apparently the IDE does not execute it in an interactive fashion, so that you cannot enter the number via the IDE.
In your IDE, you need to use the Compile or Build command (and not Run), and invoke stuck.exe manually in a command shell window.
Even my compiler is GCC-MinGW and i use Vscode, And your program works just fine even with online compilers.Maybe there is a problem with your C installation or check if your system memory near to full it might cause problems like these sometimes.

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;
}

opening an .exe after using Mingw

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.

Exiting a C program

I am new to C programming. In my program below, I am simply trying to immediately, exit the C program without see any additional dialog, if the programs receives the input "quit".
I am trying to accomplish this using exit(0); however, before the program exits it outputs something like
success
process exited with return value 0
Press any key to continue...
I am trying to avoid this dialog and exit the program immediately. Is this possible?
I appreciate any help with this.
Many thanks in advance!
My C Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void) {
char command1[256], command2[256];
printf("# ");
scanf("%s", command1);
if(strcmp(command1,"quit")==0){
printf("success");
exit(0);
}else{
printf("unknown command");
}
system("PAUSE");
return 0;
}
The message that you see is actually generated by the Visual Studio debugger. It's not really coming from your program.
If you would like to verify that your program is not actually displaying any message (nor waiting for a key press) just try running it from a windows command prompt. You may also try running the program in "Release" mode from withing Visual Studio. That will also confirm this.
The reason the debugger displays that information is just to help you understand what is going on with your program.
Can you post details of your execution environment? Seems like your process is being monitored for an exit code by another application (specialized shell perhaps) which is printing the "Press any key to continue" line
The process exited with return value 0 certainly isn't coming from your code, rather a program in the middle of your input and the output.
I compiled this on the command line (Mac OSX) and was presented with the following output:
James:Desktop iPhone$ gcc code.c
James:Desktop iPhone$ ./a.out
# quit
successJames:Desktop iPhone$
Note that I didn't reach the system("PAUSE"); either
That output doesn't come from YOUR program, it comes from the program that runs your program. Most likely "Visual Studio", but I expect some other types of IDE's may do similar things.
If you are using Dev-C++ and you would like to get rid of the message, do this:
Tools Menu -> Environment Options -> General tab
Then uncheck the Pause Program after return option.
Source: http://www.cplusplus.com/forum/general/89249/

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