I've installed NetBeans 7.0.1 today When I try to execute C program with "scanf" in it it's giving strange errors
This is what I wrote:
It keeps on running until I enter something in the output console.
After entering its shows the printf statement and shows "RUN FAILED"
Can anybody tell me what should I do to make this right?
You need to return 0 at the end of main if not it assumes there was an error.
A C program without a return value will result in undefined behaviour (which is unanimously considered a Bad Thing©). The compiler is allowed free rein in what it returns here, it appears to be returning the result of scanf() but it could be returning some atmospheric entropy for all the C Standard cares.
As for the line not printing, that's because you're using printf() on a buffered terminal, you'll want to add \n at the end. The reason for this harks back to ancient Unix ways that have long since been forgotten by all but the very sagest of Unix gurus.
As for nothing happening until you input something, that's because scanf() blocks until input is received, in case you weren't already aware of that. It is possible to use non-blocking IO calls, but I'm not sure if that's in the scope of your question. (please define "make this right").
Your printf is not getting flushed so it is not showing until the program ends.
You are not returning a value from main() explicitly, so the result of scanf() is being returned, which is 1 which is interpreted as program failure.
Add a return code.
main()
returns an int, so addreturn 0;
at the bottom of yourmain()
function. Right now, the value that gets returned is garbage, and typically any value other than 0 indicates a failure.Also, you might consider making it:
to be more explicit (though it won't change anything here).
You are not returning
0
, which indicates successful termination to the OS and you are not putting a trailing\n
on yourprintf
, causing the line to not print (stdin is buffered):Yes, I have same problem with you, and the solutions in answers are not working on my machine. After searching, I understand that this problem is about Netbean's internal terminal/console section. The internal console is not able to run scanf function. So use external terminal for your project. To do this:
That is all.