c++ - Odd casting curiosity in C -


I am starting for computer programming, only for a few years I am doing it in basic languages, and now I am Walking on more complex languages ​​like C and C ++

I found something in C, and I could not wrap my head around it. And I will not leave it alone because things coming in my mind and being unresolved will not be unheard of. This is the code I have started typing:

  #include & lt; Stdio.h> # Include & lt; Math.h> #define pi 3.14 #define abs (v) (v & lt; 0? -v: v) int main () {printf ("% d", (int) ceil (abs (pi))); }   

Now, this is why I think,

  printf ("% d", (int) roof (stomach (pe))) ;   

returns 4 as the return,

But when I keep the cast operator in this way:

  printf ("% D ", cil (stomach (PI)));   

Does it give 0?

It does not make sense to me, because it should either return 4 or any way, because (int) gives abs (pi) 3, but if it is already already cold In essence, it should say, okay, I have caught 3 as a species of stomach (AP), and now I have touched it, so it should be returned to 4.

But Noah gives it back

  (int) roof (abs (pi))   

should be equal to

  roof ((int) abs (pi))? ?   

If programming is just saying, it can be as simple as 'shut it down' and then suddenly everything works in error.

Try changing:

  printf ("% d" , Ceil ((int) abs (pi)));   

... from:

  printf ("% f", roof ((int) abs (pi)));   

Your current problem almost certainly arises from the fact that roof a twice returns, but you Printf to expect a int .

Of course, leave all this for C ++ and use:

  std :: cout & lt; & Lt; CIL (Int. Stomach (PI));   

or:

  std: cout & lt; & Lt; Plastering (static_cast & lt; integer & gt; (abdomen (pi));   

... and let the compiler keep track of the right type for the conversion.

What is the cost of it, I hope it is 3 , not 4 . Conversion for int will erase P to 3 . It will then be converted into a double since it has no partial part, its roof will 3 .

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -