54

So in python, all I have to do is

print(3**4) 

Which gives me 81

How do I do this in C? I searched a bit and say the exp() function, but have no clue how to use it, thanks in advance

samir
  • 759
  • 2
  • 8
  • 13

7 Answers7

85

You need pow(); function from math.h header.
syntax

#include <math.h>
double pow(double x, double y);
float powf(float x, float y);
long double powl(long double x, long double y);

Here x is base and y is exponent. result is x^y.

usage

pow(2,4);  

result is 2^4 = 16. //this is math notation only   
// In c ^ is a bitwise operator

And make sure you include math.h to avoid warning ("incompatible implicit declaration of built in function 'pow' ").

Link math library by using -lm while compiling. This is dependent on Your environment.
For example if you use Windows it's not required to do so, but it is in UNIX based systems.

Guillaume Jacquenot
  • 11,217
  • 6
  • 43
  • 49
Gangadhar
  • 10,248
  • 3
  • 31
  • 50
  • 3
    Couple of minor points - first, `y` is an exponent, not exponential the _result_ is exponential (it's an adjective, not a noun). Second, there's nothing in C about linking with `-lm`, that's an implementation thing and, since OP didn't state their environment, it's unwise to assume. – paxdiablo Sep 11 '13 at 06:09
  • @paxdiablo modified term exponential to exponent, added dependency on environment.thank you for pointing these. – Gangadhar Sep 11 '13 at 06:19
  • 2
    @paxdiablo The function is exponential (adjective) with change in the exponent and the result is the power (noun) of the base to the exponent. – Potatoswatter Sep 11 '13 at 06:25
  • Clarification: When using `pow()` it is always necessary to include `math.h`. What is dependent on the environment is the "link math library" step. – This isn't my real name Sep 11 '13 at 16:06
  • @ElchononEdelson when ever if you want to use pow() you should include math.h header file. Requirement of explicit linking is dependent. – Gangadhar Sep 11 '13 at 16:13
  • Yes, that's what I was saying. I see that you have edited the answer to make this more clear, which is good. – This isn't my real name Sep 11 '13 at 16:15
17

you can use pow(base, exponent) from #include <math.h>

or create your own:

int myPow(int x,int n)
{
    int i; /* Variable used in loop counter */
    int number = 1;

    for (i = 0; i < n; ++i)
        number *= x;

    return(number);
}
S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
Ryan Webb
  • 334
  • 1
  • 2
  • 7
13
#include <math.h>


printf ("%d", (int) pow (3, 4));
verbose
  • 7,827
  • 1
  • 25
  • 40
11

There's no operator for such usage in C, but a family of functions:

double pow (double base , double exponent);
float powf (float base  , float exponent);
long double powl (long double base, long double exponent);

Note that the later two are only part of standard C since C99.

If you get a warning like:

"incompatible implicit declaration of built in function 'pow' "

That's because you forgot #include <math.h>.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
5

Actually, in C you don't have an power operator. You will need to manually run a loop to get the result. Even the exp function just operates in that way only. But if you need to use that function, include the following header

#include <math.h>

Then, you can use pow().

Mehdi Charife
  • 722
  • 1
  • 7
  • 22
Bodhi
  • 314
  • 2
  • 7
  • thank you! is `include <>` the same as `import` in python? – samir Sep 11 '13 at 06:04
  • Sorry, but i don't know python, but it's same as java import, or c# using directive. And yes, the # is important! – Bodhi Sep 11 '13 at 06:08
  • 4
    @samir It's not exactly same as *import* in many languages. C `#include` just inserts contents of included file into compilation, and you could achieve identical effect by writing right declarations yourself. In other languages, *import* works in more abstract level. But purpose is the same. – hyde Sep 11 '13 at 06:12
5

For another approach, note that all the standard library functions work with floating point types. You can implement an integer type function like this:

unsigned power(unsigned base, unsigned degree)
{
    unsigned result = 1;
    unsigned term = base;
    while (degree)
    {
        if (degree & 1)
            result *= term;
        term *= term;
        degree = degree >> 1;
    }
    return result;
}

This effectively does repeated multiples, but cuts down on that a bit by using the bit representation. For low integer powers this is quite effective.

Keith
  • 6,756
  • 19
  • 23
4

just use pow(a,b),which is exactly 3**4 in python

tintin
  • 1,459
  • 1
  • 10
  • 27