Buran_
Buran_

Reputation: 3

Why can't I use integers in the pow() function?

Just a disclaimer, I am a newbie with these things. I've only really just made the leap into C, and into programming in general, so please regard that, thank you.

Hello, Stack Overflow. I have a piece of code here that for whatever reason doesn't want to work properly. The pow() function does not seem to like the way I input integers to be used, and my IDE tells me that the type of argument for argument 1 (the argument that holds an integer) is incompatible. Am I doing something wrong? Is this a beginner's mistake and I'm simply missing something? Can you use integers as arguments in the pow() function?

Here's the line of code I am using. Also, just to note I'm attempting to make a program that takes an integer and squares it, communicating to the user via printf("text") as a very basic project.

SquaredIV = pow(&InputVar, 2);

I can post more lines of code if need be, but for now I'm just going to leave this. Please help me out with this code. Thanks.

Upvotes: 0

Views: 1345

Answers (2)

Jasen
Jasen

Reputation: 12392

In c & is the address-of operatior it will yield the address of inputvar pow will not be able to do anything useful with that..

the code should have near the start.

#include <math.h>

and then later in the file

SquaredIV = pow(InputVar, 2);

THat's not actually using an integer as input to pow, what will happen is that the compiler will find the definition of pow in math.h and know it needs to convert the int into a double (double precision floating point) and send that to pow. Pow will return a result as a double and the compilwe will know to convert this into whatever type SquaredIV is.

The usual (ans most efficient) way to square an integer is to multiply it by itself.

SquaredIV = InputVar*InputVar;

Upvotes: 1

ouah
ouah

Reputation: 145829

Can you use integer as arguments to pow function?`

Yes, you can. The integer value is implicitly converted to a floating point value of the right type.

In your case:

SquaredIV = pow(&InputVar, 2);

the problem is &InputVar is not of an integer type but of a pointer type as & unary operator yields a pointer to the operand object.

Upvotes: 2

Related Questions