Programs with scanf do not work properly in NetBeans

I installed NetBeans 7.0.1 today. When I try to execute a C program with "scanf" in it, it gives strange errors.

Here is what I wrote:

Program

It continues to work until I find something in the output console. enter image description here

After entering its readings, the printf command is issued and the message "RUN FAILED" is displayed.

enter image description here

Can anyone tell me what I have to do to get it right?

+6
source share
6 answers

Your printf is not cleared, so it is not shown until the program finishes.

You are not returning the value from main () explicitly, therefore the result of scanf () is returned, which is 1, which is interpreted as a program crash.

+3
source

Yes, I have the same problem with you, and the solutions in the answers do not work on my machine. After searching, I understand that this problem is related to the Netbox section of the internal terminal / console. The internal console cannot run the scanf function. Therefore, use an external terminal for your project. For this:

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

That's all.

+8
source

You need to return 0 at the end of the main, unless it is assumed that an error has occurred.

+1
source

You do not return 0 , which indicates a successful shutdown of the OS, and you do not put 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; } 
0
source

A C program without a return value will result in undefined behavior (which is unanimously considered bad type ©). The compiler is allowed to freely control what it returns here, it seems to return the result of scanf (), but it can return some atmospheric entropy for all the cares of C Standard.

As for the line not printing, because you are using printf () on a buffered terminal, you need to add \ n to the end. The reason for this comes down to the ancient ways of Unix, which have long been forgotten by everyone except the sagist of the Unix guru.

Like nothing happens until you enter something, because scanf () is blocked until you get input, in case you did not know about it yet. Non-blocking I / O calls can be used, but I'm not sure if this concerns your question. (please specify "do it right").

0
source

Add return code. main() returns int, so add return 0; at the bottom of your main() function. Right now, the return value is garbage, and usually any value other than 0 indicates a failure.

Alternatively, you might think about this:

 int main(void) 

to be more explicit (although this will not change anything here).

-1
source

Source: https://habr.com/ru/post/907501/


All Articles