-4

I'm starting to write a C code after a gap of almost 10 year. I was teaching my sister and unfortunately got stuck at a very basic scanf() function for a simple interest program on Ubuntu.

My program is not giving any compilation error but it's not showing anything on screen

Here is the code:

#include <stdio.h>
int main()
{
    int p,n;
    float r,si;
    printf("enter values of p,n,r");
    scanf("%d %d %f",&p,&n,&r);
    si = p*n*r/100;
    printf("%f" , si);
    return 0;
 }
  • 4
    What do you mean by "scanf ... not working"? You never check the return value, so how do you know it's not working? – Andrew Henle Jul 06 '18 at 16:55
  • 1
    It works for me! Enter the values separated by whitespace, not commas – Antti Haapala -- Слава Україні Jul 06 '18 at 17:02
  • You're probably not seeing the output because it's on the same line as the next shell prompt. Add a newline at the end: `printf("%f\n", si)` – Barmar Jul 06 '18 at 17:15
  • Several answers suggest either printing a newline or calling `fflush(stdout)` after the first `printf` call. That's certainly a good idea, but on my Ubuntu system, when I run the program from a shell prompt, the program works without that; the prompt is printed correctly. And the answer is printed, but without a newline, so the next shell prompt appears on the same line. When you say it's "not showing anything", do you mean the "enter values ..." prompt doesn't appear? How exactly are you running the program? – Keith Thompson Jul 06 '18 at 17:40
  • To be clear: Does the "enter values" prompt appear? If it does, then your statement that "it's not showing anything on the screen" is incorrect. If it doesn't, then you're having problems before the program even gets to the `scanf`, so you shouldn't assume that the `scanf` is the problem. – Keith Thompson Jul 06 '18 at 17:42

4 Answers4

2

This is a common "issue" when trying to printf() and scanf() after one another. The operating system buffers stdin and stdout in order to increase performance and only flushes them when necessary. You can explicitly flush stdout by calling fflush(stdout); right after printf().

See C/C++ printf() before scanf() issue.

Your code would end up looking like

#include <stdio.h>
int main()
{
    int p,n;
    float r,si;
    printf("enter values of p,n,r\n");
    fflush(stdout); // Force stdout to be flushed
    scanf("%d %d %f",&p,&n,&r);
    si = p*n*r/100;
    printf("%f" , si);
    return 0;
 }
Slygga
  • 46
  • 3
  • 1
    As important as, or more important than, the `fflush(stdout)` is adding a newline to the end of the `printf()` statement. That gets the output to the screen in a sensible, timely manner. You could add a `fflush(stdout)` too — but the newline is still a good idea. (And printing the input values is also a good idea, as is checking that `scanf()` returns 3, because if it doesn't, you've got an input failure and uninitialized variables in the calculation.) – Jonathan Leffler Jul 06 '18 at 17:49
0

I think you are just not looking at your console properly. The output "si" is getting printed on the same line as your first printf since you do not have a newline inserted at the end of it.

0

Your code worked perfectly for me in my Ubuntu system.This may be because of the mistake of the IDE you are using.

Try running code in Ubuntu this way:-

  • save file as program.c on desktop.
  • open terminal and type cd Desktop (navigate to desktop).
  • compile code by typing gcc -o program program.c .
  • run program by typing ./program.
anoopknr
  • 3,177
  • 2
  • 23
  • 33
0

When you say

not showing anything on screen

(emphasis added), I take you to mean that when you run it, it's not displaying the data-entry prompt you're printing. This would be because the standard output is line-buffered by default when connected to a terminal -- it will buffer the output in memory before printing it, until either the buffer is filled, or (because line buffered) a newline is output, or the stream is closed (including when the program terminates normally).

One way to cause the prompt to be displayed, therefore, is to append a newline to it:

printf("enter values of p,n,r\n");

Another is to use an output function that automatically appends a newline:

puts("enter values of p,n,r");

If you want to ensure that the output appears even though no newline has been sent then you can flush the stream (standard output in this case) instead:

printf("enter values of p,n,r: ");
fflush(stdout);
John Bollinger
  • 160,171
  • 8
  • 81
  • 157