Programs with the scanf not working properly in Ne

2020-02-12 05:53发布

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:

Program

It keeps on running until I enter something in the output console. enter image description here

After entering its shows the printf statement and shows "RUN FAILED"

enter image description here

Can anybody tell me what should I do to make this right?

标签: c netbeans scanf
6条回答
叛逆
2楼-- · 2020-02-12 06:04

You need to return 0 at the end of main if not it assumes there was an error.

查看更多
Summer. ? 凉城
3楼-- · 2020-02-12 06:06

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").

查看更多
萌系小妹纸
4楼-- · 2020-02-12 06:12

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.

查看更多
三岁会撩人
5楼-- · 2020-02-12 06:17

Add a return code. main() returns an int, so add return 0; at the bottom of your main() 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:

int main(void)

to be more explicit (though it won't change anything here).

查看更多
霸刀☆藐视天下
6楼-- · 2020-02-12 06:30

You are not returning 0, which indicates successful termination to the OS and you are not putting a trailing \n on your printf, causing the line to not print (stdin is buffered):

#include <stdio.h>

int main()
{
    int n;
    printf("Enter the number:\n");
    scanf("%d", &n);
    return 0;
}
查看更多
甜甜的少女心
7楼-- · 2020-02-12 06:31

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:

  • first right click on your project, and select properties.
  • In that window select "Run" tab at the bottom.
  • in there, there is "Console Type", change this console type from "internal terminal" to "external terminal".

That is all.

查看更多
登录 后发表回答