Why Ecplise (CDT) console and debug don't work properly? - c

I have the following problem with a C code in Eclipse. This is the minimal code:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
char inputch[20];
fgets(inputch,20,stdin);
printf("%s", inputch);
getchar();
return EXIT_SUCCESS;
}
The code is built correctly and if I run it from the built .exe, it works properly: I write a line and press ENTER, then the line entered is showed in the console and then if I press any button in the keyboard the console shutdown.
But the behaviour is different if I run the program in the "run mode of eclipse": when I write the line and I type ENTER, nothing happen; when I type ENTER once again, the line I wrote before appears in the console and the program ends. If I wrote something between the first and the second ENTER, this line doesn't appear in the stamp; however, the stamp appear always at the second ENTER typing, the same that ends the programs, like the getchar and the printf are inverted.
The second problem is in debug mode: if I try to debug the code, if I put a breakpoint in the fgets() function and try to step into, the debug console doesn't permit me to write the entry in the STDIO, but step immediatly at the next statement.
MY CONFIGURATION: eclipse Neon 3 4.6.3 x64, CDT, Cygwin compipler x64, windows 8.1 64 bit
EDIT: I tried also with miniGW but the problem persist. The debugger, in both case, is GDB 7.6.11

Related

Why does this show after running my C program?

So, after running this code in Visual Code:
#include <stdio.h>
int main(){
printf("Hello!");
return 0;
}
I get this in my console:
image
What's up with all that added text?
Because you're running your program under Visual Studio Code's debugger, and for some reason it prints out a bit of job control trash on the console.
Maybe add a newline at the end of your print, to make your output clearer:
printf("Hello!\n");
Alternatively, don't run under the debugger (look for a "Run without debug" option).

Can't indicate EOF through keyboard in Windows (Netbeans) , in a C program [duplicate]

I have been learning C from K&Re2. And the above code is what is mentioned in Pg18(Letter counting program), which I ran for confirmation purposes. I tried entering few characters and press ENTER, but it was not working. Then I heard about CTRL+Z,CTRL+C or CTRL+D with ENTER for End Of File. I tried it in NetBeans console, but it was not working. I tried \0 and \n too, pity it was not working too. I have searched for this, but all seemed to have solved the problem with CTRL+Z,CTRL+C or CTRL+D with ENTER method. I can't understand what is the problem here.
PS: I use Windows 7
Sorry for not inserting code directly. Here is it-
#include <stdio.h>
#include <stdlib.h>
int main() {
long c = 0;
while (getchar() != EOF) {
++c;
}
printf("%ld", c);
return 0;
}
In the image, I have not initialized value of long c. Sorry for that. This program is running, but the methods I use for EOF doesn't work out.
EDIT:
I have tried compiling in NetBeans, and then running the resulting .exe in cmd rather than in NetBeans console. CTRL+Z seems to work! Do you guys have any idea why it doesn't work in NetBeans console?
getchar() stores characters in buffer until you press enter key. After enter key is pressed,first character is taken from buffer if no subsequent variable is being assigned.As you used while loop it will take until \r\n.so you have to press enter key + ctrl+z to reach EOF.
Windows Only
Product Version: NetBeans IDE 8.2 (Build 201609300101)
Updates: NetBeans IDE is updated to version NetBeans 8.2 Patch 2
Run > Set project configuration > Customize...
Category = Run
Console Type = External Terminal
External Terminal Type = Command Window
Click Apply then OK
Run project
To send EOF press ENTER then CTRL+D Or Press CTRL+D twice

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/

C program output in wrong order Eclipse

I have set up Eclipse for c programming on my Windows machine, I have successfully run a "hello, world" program. However, when I try to ask for user input and run the program the console on Eclipse is displaying in the wrong order.
Here is what I have
#include <stdio.h>
int main(void){
char letter;
printf("Please enter a letter:\n");
scanf(" %c, &letter);
printf("The letter you have selected is: %c", letter);
return 0;
}
This program builds just fine, and it runs just fine outside of Eclipse. But when I run it in Eclipse I get the output:
E <--- (this is my user input)
Please enter a letter:
The letter you have selected is: E
I'm not sure why the output is executing in the wrong order, so any help would be much appreciated! Thank you.
Yeah, Eclipse will buffer a certain amount of output (I don't remember how much off hand) before it will appear in the output window. Eclipse is communicating with the attached process through a pipe which is fully buffered. It won't flush until either fflush() is called or the buffer is full. I found that when debugging with Eclipse, things work best if I put the following near the beginning of my application:
setvbuf(stdout, NULL, _IONBF, 0);
This will cause stdout to flush immediately whenever it is written to. If you want to use that for debugging and turn it off otherwise, you can conditionally compile it:
#ifdef DEBUG
setvbuf(stdout, NULL, _IONBF, 0);
#endif
No need to put fflush() everywhere this way.
Edit
Here's where I found the solution when I first ran into this issue myself.
http://wiki.eclipse.org/CDT/User/FAQ#Eclipse_console_does_not_show_output_on_Windows
Eclipse's console is not a true console or terminal but rather eclipse is communicating with the attached process through a pipe which is fully buffered not line buffered. This is why a newline '\n' does not cause the buffer to be flushed.
It sounds like Eclipse is buffering the output of your program and not displaying it right away. This indicates that the "run within Eclipse" feature is not intended to run interactive programs.
You could try adding fflush(stdout); after the first printf, but you shouldn't have to do that just to make your program work in a particular environment.
Try adding fflush(stdout); after the first printf. This has a decent chance of being of help, in case Eclipse does not auto-flush after '\n'.
Yes, fflush()ing buffers is necessary to keep the console's screen updated ...
... but please guys, it's not Eclipse's fault in- and output might get out of sync, but the library's in use!

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