# ultra-noob question from "let us c" by yashavant kanetkar

• 11-29-2008
newbcore
ultra-noob question from "let us c" by yashavant kanetkar
Hi, I have no prior programming experience and am teaching myself C through this book. I am trying to get this program to work. Here it is:

Code:

```#include <math.h> main() {       int a;       a=pow(3,2);  /*I don't know why this doesn't work*/       printf("%d",a);             }```
The screen at the bottom of the compiler (Bloodshed Dev-C++) says:

[Warning] converting to 'int' from 'double'

The line I commented on is highlighted in red in the compiler. The program is to raise 3 to the power of 2.

Thanks!
• 11-29-2008
Dino
The message says the pow() funtion returns a data type of double, not int.
• 11-29-2008
newbcore
Quote:

Originally Posted by Dino
The message says the pow() funtion returns a data type of double, not int.

Thanks. Is there is another function I can use to return an int? Alternatively, if there's some better way to fix it, I'm appreciative of any suggestions.
• 11-29-2008
Dino
You can cast the pow() function to return an int.
• 11-29-2008
newbcore
How do you do that?
• 11-29-2008
blurx
Code:

`a = (int) pow(3,2);`
Try that.
• 11-29-2008
newbcore
Quote:

Originally Posted by blurx
Code:

`a = (int) pow(3,2);`
Try that.

Thanks everyone. I got it to work by doing what I've posted below. Is that part at the top correct (with the two #includes)? I did it because after I followed blurx's suggestion it highlighted the printf line and said printf was undeclared. As you can see, I started to learn C 2 or 3 days ago, so I'm appreciative of all the help.

Code:

```#include <math.h> #include <stdio.h> main() {       int a;       a = (int) pow(3,2);  /*This works now.*/       printf("&#37;d",a);       getchar();             }```
• 11-29-2008
newbcore
Quote:

Originally Posted by blurx
Code:

`a = (int) pow(3,2);`
Try that.

Also, why does this work?
• 11-29-2008
stevesmithx
Quote:

Originally Posted by newbcore
Also, why does this work?

Because pow() function returns double and
you are casting it int.[Note that this may result in loss of precision if arguments
supplied in pow are double].

Also main returns int.
implicit main should not be used.
http://faq.cprogramming.com/cgi-bin/...&id=1043284376