power function in C | Unix Linux Forums | Programming

  Go Back    

Programming Post questions about C, C++, Java, SQL, and other programming languages here.

power function in C


Closed Thread    
Thread Tools Search this Thread Display Modes
Old 10-30-2001
krishna krishna is offline
Registered User
Join Date: Aug 2001
Last Activity: 3 August 2007, 4:25 AM EDT
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
power function in C


I wrote a small program to find 2 to the power of 3,
when I tried to execute the following error occured.
How can I solve this problem?

$ cat t1.c
#include <math.h>

int a=2;
int b=3;
int c;
c = a ^ b; /* if i use ** instead of ^ also the same problem */

$ cc -o t1 t1.c

$ a.out
Floating exception(coredump)

Sponsored Links
Old 10-30-2001
Posts: n/a

but the ^ does not translate to "TO THE POWER OF ..." at all. It is only an XOR Bitwise Operator. you could write your own function for this purpose or use the pow() built-in function from the math.h library.

one more thing, why are you using %f in your printf() while printing an integer?

Sponsored Links
Old 10-31-2001
guru guru is offline
Registered User
Join Date: Oct 2001
Last Activity: 8 November 2001, 8:20 PM EST
Location: San Jose
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
hey, just like shaik said, i would also use pow(). but if you are dying to have your own here is a simple prog.

int i;
int a=2; //you can choose any value for a and b
int b=3;

int c=a;


also, if you do cc -o t1 t1.c, then your binary is gonna be t1. if you just do cc t1.c, then your binary is a.out (default)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
SHELL SCRIPT Function Calling Another Function Please Help... omkar.sonawane Shell Programming and Scripting 2 04-13-2010 11:20 AM
Return a value from called function to the calling function mvictorvijayan Shell Programming and Scripting 1 09-14-2009 05:19 AM
Passing global variable to a function which is called by another function sars Shell Programming and Scripting 4 06-30-2008 12:39 PM
Need help in using power function in Bash ahjiefreak Shell Programming and Scripting 3 03-26-2008 05:31 AM

All times are GMT -4. The time now is 09:07 AM.