0
  int x;
  scanf("%d",&x);
  x=(double)((x*3.14)/180);
  printf("%.6lf",x);

When I run the above code and input x=60, I get -0.000000.

  int x;
  double a;
  scanf("%d",&x);
  a=(x*3.14)/180;
  printf("%.6lf",a);

But when I run this above code, I get the correct answer.

I want to know where I am doing wrong. Is there problem in my type casting or use of double or any other thing? Any help will be appreciated. Thanks!

N.B. : I need to print output upto 6 digits after decimal.

Nehal Samee
  • 166
  • 3
  • 10
  • 4
    `x` is an `int`. You can't make it a `double` at your will. – Paul Ogilvie May 22 '19 at 08:41
  • 2
    Unlike other languages (e.g. python), C does not change the type of a variable to match the value. Instead it changes the type of the value to match the variable. Since `x` is declared as an `int`, any value assigned to `x` will be converted to an `int`. – user3386109 May 22 '19 at 08:41
  • `3.14`?͏͏͏͏͏͏͏͏͏͏͏͏͏ – Bathsheba May 22 '19 at 08:43
  • @Bathsheba...its pi im using – Nehal Samee May 22 '19 at 08:45
  • You're typecasting into double but `x` is still int and when you print with `lf` it expects a double but it is still an integer. Compile with the `-Wall` flag next time to gain better insight. – Pratik Sampat May 22 '19 at 08:45
  • 2
    You're not using `pi`. You're using `3.14`. – Bathsheba May 22 '19 at 08:46
  • @Pratik.. Can you provide any insight of how using a -Wall flag? – Nehal Samee May 22 '19 at 08:47
  • @NehalSamee. `gcc -Wall program.c` will show some insightful warnings you may have not noticed. Also following @Bathsheba suggestion, if you want to use pi, include `math.h` and use `M_PI` instead. – Pratik Sampat May 22 '19 at 08:51
  • 1
    @PratikSampat: Except that `M_PI` is not required by the standard, so your code would not be portable. Oh joy! – Bathsheba May 22 '19 at 08:58
  • @Bathsheba, or you could just `# define M_PI 3.14159265358979323846` which is what's in the `math.h` source? – Pratik Sampat May 22 '19 at 09:06
  • @PratikSampat: See one of my better answers, written on the C++ tag, but the sentiment applies to C: https://stackoverflow.com/questions/49778240/does-c11-14-17-or-20-introduce-a-standard-constant-for-pi/49778493#49778493 – Bathsheba May 22 '19 at 09:16
  • Ah neat! I've learned something new today. Thanks @Bathsheba! – Pratik Sampat May 22 '19 at 09:21

1 Answers1

5

There are two problems in your code:

x = (double)((x*3.14)/180);

(x*3.14)/180 is already a double therefore this line is equivalent to:

x = (x*3.14)/180;

but anyway the type of x remains int, so if x was e.g 300, the new values of x will be 300 * 3.14/180 = 5.2333 which will be trunacated to 5.

The second problem is here:

printf("%.6lf",x);

As explained before, the type of x is int, but the "%.6lf" format specifier requires a double. If the variable types don't match the format specifier, the behaviour is undefined.

The second version of your code is perfectly correct, but be aware that the user can only enter integer values.

BTW: 3.14 is a very poor approximation of PI, I'm sure you can do better.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115