0

I'm learning c via a guide book and i am enjoying it. However, there is one question that i am stuck.

The question is:

"Write a program that when writing number "x" and number "y", the program shows how much % of x is in y."

"The answer should be 64 % when x = 54 and y = 84"

Obviously, 54 / 84 = 0.64... * 100, which is about 64 %. However, when I run my program it shows 84.689699. I tested without the "*100" but nothing. It shows 0.84689699...

Is my program wrong or is it a problem of the compiler or something? I am a beginner and it would be very helpful if someone tells me what is wrong.

PS: I use atom.io and gcc-compiler

#include <stdio.h>

int main(void)
{
  double vx;
  double vy;

  printf("Enter the 1st number : "); scanf("%f" , &vx);
  printf("Enter the 2nd number : "); scanf("%f" , &vy);

  printf("\a\n\nx is %f of y" , vx / vy * 100);
  return 0;
}
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
Pingu
  • 13
  • 3
  • I also tried with 100.0 instead of 100 but nothing... – Pingu Aug 30 '17 at 19:07
  • 6
    Read the warnings: http://coliru.stacked-crooked.com/a/2f51b696c1f63219 – tkausl Aug 30 '17 at 19:08
  • 2
    %lf (lowercase L) not %f – pmaxim98 Aug 30 '17 at 19:10
  • 3
    @Pingu Please stop adding the C++ tag. You say your are using C so there is no need for the C++ tag. If you are actually using C++ then you should say so and remove the C tag. – NathanOliver Aug 30 '17 at 19:11
  • 1
    See [why scanf needs %lf for doubles](https://stackoverflow.com/questions/210590/why-does-scanf-need-lf-for-doubles-when-printf-is-okay-with-just-f) – AJNeufeld Aug 30 '17 at 19:12
  • @tobi303 he is obviously using C and not C++ and should remove the C++ tag. To the question: Your compiler should give a warning that you use a wrong format specifier. Use `%lf` instead of `%f` and it should work. – dtell Aug 30 '17 at 19:13
  • Yes, I know this is not c++ and undersdtand the difference between those 2 languages. – Pingu Aug 30 '17 at 19:13
  • I didn't know the stackflow community was so huge. Answers came instantly – Pingu Aug 30 '17 at 19:23

2 Answers2

1

Although scanf is a variadic function the input can not be promoted.scanf takes a pointer as an input, therefore you need to specify it will %lf. If the input was a variable, not a pointer, C would promote float to double. In your program scanf function has %f instead of %lf. Below code works fine and the output is 64.285714 on MinGW.

Also, refer to link Correct format specifier for double in printf

int main(void)
{
  double vx;
  double vy;

  printf("Enter the 1st number : "); scanf("%lf" , &vx);
  printf("Enter the 2nd number : "); scanf("%lf" , &vy);

  printf("\a\n\nx is %f of y" , vx / vy * 100);
  return 0;
}
MCG
  • 1,011
  • 1
  • 10
  • 21
0

You made a mistake in your scanf function.

%f in a scanf function means your input will be put into a float variable.

use a %lf instead.

#include <stdio.h>

int main(void)
{
  double vx;
  double vy;

  printf("Enter the 1st number : "); scanf("%lf" , &vx);
  printf("Enter the 2nd number : "); scanf("%lf" , &vy);

  printf("\a\n\nx is %f of y" , vx / vy * 100);
  return 0;
}