0

I wrote a simple code to find even and odd numbers in c.

#include <stdio.h>
int main() {
   int num;
   printf("Enter an integer: ");
   scanf(" %d", &num);

(num % 2 == 0) ? printf("%d is even.", num) : printf("%d is odd.", num); return 0; }

The code starts running without stopping and gives message like this

[Running] cd "/home/atharva/Projects/c c++/" && gcc Even_odd.c -o Even_odd && "/home/atharva/Projects/c c++/"Even_odd

I am using ubuntu.What should be done?

Chris Davies
  • 116,213
  • 16
  • 160
  • 287
  • That command is just the build-and-run process. So it gets run. I think it is waiting for you to enter an integer. It probably does not prompt because it is buffered: try adding a \n at the end of the printf string. – Paul_Pedant Dec 21 '21 at 16:25
  • No, the output dosen't even show printf statement and nor is asking for the integer – ATHARVA SHETTY Dec 21 '21 at 16:35
  • I didn't say it is asking for the integer. It didn't ask, but it is (probably) waiting for input. Just type 5 and see what happens ! If not, just put printf ("Hello, World.\n"); return (0); as the first line under main. If it can't even do that right, your IDE is broken. – Paul_Pedant Dec 21 '21 at 17:34

2 Answers2

0

Your example works for me at the command line.

Copy your code to Even_odd.c and then

make Even_odd    # A quicker alternative to "gcc Even_odd.c -o Even_odd"
./Even_odd
Enter an integer: 

If you're not really running from the command line you might need fflush(stdout) between the printf and the sscanf. This is because stdout is line buffered and the line may not get flushed (written) to the terminal before the input is requested by sscanf.

Don't forget \n at the end of your result strings, for example, printf("%d is even.\n", num)

Chris Davies
  • 116,213
  • 16
  • 160
  • 287
  • If I add fflush(stdout) in the code, it executes till printf statement but i am unable to enter an integer. It is not identifying any keystrokes. – ATHARVA SHETTY Dec 21 '21 at 16:48
  • Nothing I know of outputs that [Running] message -- certainly not a shell. What IDE (development environment) are you using? Maybe it is launching the code in the background (which would prevent it reading from the terminal). – Paul_Pedant Dec 21 '21 at 17:39
  • @Paul_Pedant my thought too, although perhaps "If you're not really running from the command line" is too subtle..? – Chris Davies Dec 21 '21 at 17:46
  • @roaima Obviously too subtle, especially for me ;-) Personally, I would check stdin is a terminal before I start putting up prompts, and also fprintf/fflush to stdin for prompts. But I would expect stdio to flush all buffers on exit regardless. – Paul_Pedant Dec 21 '21 at 17:51
  • 1
    It does flush on edit, but an output without a newline messes up the prompt – Chris Davies Dec 21 '21 at 18:49
  • Maybe visual studio code has an option to run the program with its descriptors connected to a tty. – Mark Plotnick Dec 21 '21 at 23:42
0

Your program is running, but you won't see any output from your program because the output is buffered.

The output is buffered because the string that you have outputted with printf() is not delimited by a newline at the end. The standard library will buffer the output until a newline is outputted or until you explicitly call fflush() on the output stream. This buffering is done for efficiency.

One solution is to either output the string with a newline at the end:

printf("Enter an integer:\n");

... or to explicitly call fflush():

printf("Enter an integer: ");
fflush(stdout);

However, the correct way of doing it is to output any text intended for user interaction on the standard error stream, like most other Unix utilities do:

fprintf(stderr, "Enter an integer: ");

The standard error stream is unbuffered by default.

The other printf() statements in the code suffers from the same issue and should probably have newlines appended to the strings they output (as they output the result, not user interaction messages).

See also:

Kusalananda
  • 333,661
  • now it is executing till printf but now the problem is I am unable to type integer in inout – ATHARVA SHETTY Dec 21 '21 at 17:07
  • 1
    @ATHARVASHETTY I'm assuming that you are able to but that you don't see the output. This is due to the buffering again. You have multiple printf() statements with the same issue. – Kusalananda Dec 21 '21 at 17:11