PDA

View Full Version : Arithmetic type conversion in C



tauh
September 14th, 2011, 02:11 PM
Hi! For several days i have been struggling with a homework in C programming. I have Google it, my teacher wont answer my emails and the book i have doesn't talk that much about type conversions.

http://i54.tinypic.com/2e5ingh.jpg

double a;
float b;
int c;

b / c?
(long)a?
(long)a + b / c?
What type will C return?

I am pretty sure about the first one, b / c will return a float.. Correct?

And (long)a will give me a long double? Then (long)a + b / c should give me a long double to? But according to the webpage where i write the answers it tells me thats wrong?

ofnuts
September 14th, 2011, 02:41 PM
Hi! For several days i have been struggling with a homework in C programming. I have Google it, my teacher wont answer my emails and the book i have doesn't talk that much about type conversions.

http://i54.tinypic.com/2e5ingh.jpg

double a;
float b;
int c;

b / c?
(long)a?
(long)a + b / c?
What type will C return?

I am pretty sure about the first one, b / c will return a float.. Correct?

And (long)a will give me a long double? Then (long)a + b / c should give me a long double to? But according to the webpage where i write the answers it tells me thats wrong?
There is a policy here about doing someone else's homework :) However, the -Wconversion option of GCC will tell you a lot about the conversions that occur, so if you submit the right code to the compiler you'll have all your answers...

tauh
September 14th, 2011, 03:05 PM
There is a policy here about doing someone else's homework :) However, the -Wconversion option of GCC will tell you a lot about the conversions that occur, so if you submit the right code to the compiler you'll have all your answers...

Hi thank you for your answer. I don't want someone to do my homework by giving me the answers. Only to be help me solve it myself. :)

slavik
September 14th, 2011, 03:34 PM
C will convert types to the kind that can store more information. Google is a great help, too. http://techpubs.sgi.com/library/dynaweb_docs/0650/SGI_Developer/books/CLanguageRef/sgi_html/ch05.html

Off Shore
September 16th, 2011, 11:35 AM
Itds a nice question you've been set and one which is designed to make you think about how your compiler behaves.
I would assume, if he is worth his salt,your teacher would advise you to find out exactly what your compiler returns.
You need to be aware that not all compilers handle implicit type conversions in the same way. In fact some may return quite surprising results.
Your best course of action ? Submit a code fragment to your compiler and then you will have the answer to your assignment.
An enquiring mind would then proceed to wonder how that answer came about, and Im certain thats exactly what you will do.
Good luck

kr0n1x
September 16th, 2011, 11:08 PM
http://en.wikipedia.org/wiki/Type_conversion

maybe this could also help you

http://www.iso-9899.info/wiki/Books (you can check the free resources)