Error running *.c file in Eclipse Kepler - c

I've started programming in C so weeks ago, and I choose Eclipse Kepler as my IDE for C, since I had already used it for programming in some other languages and really liked.
However, after I had installed Cygwin and the C programming tools in Eclipse, I tried to run the old "Hello World" and it didn't work, It didn't appeared anything in the Console, only the message "Terminated".
#include <stdio.h>
int main(){
printf("Hello World!");
return 0;
}
Anyone has any idea what the problem could be?
Thanks to all of you. I tried everything you said but I wasn't able to get my problem solved. I gave up! Right now, i'm using as workspace in eclipse the Cygwin home. I write the program in the Eclipse and run it in the Cygwin command line.
Again, thanks to all for trying to help, it won't be forgotten. You're a really awesome crowd!

Maybe the program was too fast for you to see it?
The window with the program output appeared and disappeared in the blink of an eye.
You could try to prevent that from happening by changing your program to do its stuff AND wait for ENTER before it terminates.
#include <stdio.h>
int main(){
printf("Hello World!");
getchar(); /* simple wait for ENTER, error prone in more complicated programs */
return 0;
}
Note: your original program "prints a string"; this version "prints a string and waits for ENTER". It's a program with different requirements. If you want to keep to your initial requirements, try running the program from the DOS console.

you could try to system("pause") may this can help
#include <stdio.h>
int main(){
printf("Hello World!");
system("pause");
return 0;
}

Related

How to create a C console application in Visual Studio Code

I haven't found any extension, or tutorial to make and execute a C console application in the VSCode Terminal.
A simple program like
int main()
{
printf("Hello World!");
return 0;
}
And have the output in the VSCode Terminal.
Does someone know how to realize this? And/or are there solutions?
Thanks in advance
Regards
There actually is an extension for c/c++ on VSCode:
When you click the arrow in the top right (to run the file) you will be asked which compiler you want to use. In our case we can use the gcc compiler:
Then you can paste your code into a .c file and run it with the compiler. It should automatically also execute the binary and print your output into the debug-console:
#include <stdio.h>
int main() {
printf("Hello World!");
return 0;
}
Hello World!
You even have a debugger, if you set certain breakpoints!
Extra:
Make sure that you have the correct OS set in the bottom right (in the status bar), so your c code compiles for your machine specifically.

How to compile C program in Visual Studio Code?

I installed VSC and wrote this program
#include <stdio.h>
int main( void )
{
printf("Hello world!");
}
Then I installed a C/C++ debugger and I saved the file on the desktop as "hello.c" and magically the syntax became higlighted.
Now I would like to run the program, but can't find a way.
There's no "run" or "build" option anywhere, and if I start the debugging and choose the debugger then I got a new tab named "settings.json" with a couple of brackets waiting for me to write something.
Also in the left I can read that I "have not yet opened a folder". What does it even mean?
And the line #include <stdio.h> is underlined in red, like there's something wrong going on, maybe I have to download the library?
I would do anything to see the output of the program, please help me.
have you added c/c++ and code runner extensions?

getmaxyx() Error in Code::Blocks

This message appears when i run my program through Code::Blocks
And Here is my code.I am not trying to create something huge , for now i want to figure out what pdcurses functions do.
#include <stdio.h>
#include <stdlib.h>
#include <curses.h>
#include <string.h>
int main()
{
int maxx=80, maxy=54;
initscr();
getmaxyx(stdscr, maxy, maxx);
printw("maxy= %d maxx= %d \n", maxy , maxx);
refresh();
getch();
endwin();
return 0;
}
I'm pretty sure it's failing on initscr(), rather tham getmaxyx(). This looks like another variation of the problem you posted here, where the IDE is giving you only a partial console environment to run in, as part of its attempt to keep things integrated. I'm not a user of either Code::Blocks or Eclipse, so maybe I'm off-base here, but that's what it looks like to me.
So again, try manually opening a cmd window from the OS, and running the program from there. (Or, starting the program from Windows Explorer should automatically create a console window.)

C90 printf with \n or \r\n not working in cygwin; but fflush(stdout); works fine. why?

Cygwin64 bit
Command to compile:
gcc hello.c -o hello -ansi -pedantic-errors
Command to run
./hello
hello.c
#include<stdio.h>
int main() {
/*setbuf(stdout, 0); I KNOW THIS WILL WORK IF ADDED, it is a solution, but I want to know why line break itself is not working*/
printf("hello world!\n");
printf("hello world again!\r\n");
/*fflush(stdout); without fflush, the above strings are not showing*/
while(1)
{
}
}
Questions:
I don't want a fflush after every printf to let the terminal show the string in time, then how?
ANSWER: setbuf(stdout, 0);
Why is "\n" or "\r\n" not working in my case considering lots of posts pointed out a line break will fix the problem?
Is it true that cygwin's terminal behaves differently than normal Linux's terminal? Since I don't have linux installed, anyone give a test for me?
Or let me ask a more general question: On which kinds of terminals, the sentence "a new line will force flush" is true?
Thanks
It seems that in Cygwin, stdout isn't identified as a terminal (but as a pipe), so it isn't line-buffered by default.
Based on this answer, perhaps linking with the Cygwin DLL would help.
That program works for me under 32-bit Cygwin. Specifically, when I compile and execute this program:
#include<stdio.h>
int main() {
/*setbuf(stdout, 0); I KNOW THIS WILL WORK IF ADDED, it is a solution, but I want to know why line break itself is not working*/
printf("hello world!\n");
printf("hello world again!\r\n");
/*fflush(stdout); without fflush, the above strings are not showing*/
while(1)
{
}
}
it produces this output:
hello world!
hello world again!
and then hangs until I kill it with Ctrl-C.
I get the same behavior invoking the program from bash under the Windows console, mintty, and xterm (I doubt that the terminal would make any difference).
I'm using 32-bit Cygwin under 64-bit Windows 7. You say you're using 64-bit Cygwin, which was just announced a few days ago; I haven't tried it yet.
I suspect an issue with 64-bit vs. 32-bit Cygwin. I suggest you post to the Cygwin mailing list.
Here's a cleaned-up version of your program that should exhibit the same issue (please verify that the comments are correct):
#include <stdio.h>
int main(void) {
/* Adding setbuf(stdout, 0) here makes the output appear */
printf("hello world!\n");
/* Adding fflush(stdout) here makes the output appear */
while(1) {
/* nothing */
}
}
I know nothing about cgywin. but here I do a test in Linux.
I try the code below, and compile by : gcc -std=c90 filename.c
Without fflush It print all words before loop,so I think the newline flush the buffer! It just work!
I use SUSE gcc .
#include<stdio.h>
int main() {
/*setbuf(stdout, 0); I KNOW THIS WILL WORK IF ADDED, it is a solution, but I want to know why line break itself is not working*/
printf("hello world!\n"); /*without fflush, not shown*/
printf("hello world again!\r\n"); /*without fflush, not shown*/
/* fflush(stdout);*/
while(1)
{
}
}
I've just had this problem. I've got some code I've been working on a while and had to reinstall Cygwin on my PC. I installed the 64-bit version this time, it was x86 previously.
Built my code, ran it in the bash shell and no output.
What I didn't realize is that I have a Cygwin1.dll from the previous 32-bit version of cygwin still in my bin folder. I renamed this and ran my program and the output worked.
You may have a different issue, but if you've got different dll versions in use your bin other folders, it may cause this problem.

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/

Resources