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

Programming


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
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

Hi,

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>
#include<stdio.h>

main()
{
int a=2;
int b=3;
int c;
c = a ^ b; /* if i use ** instead of ^ also the same problem */
printf("%10f",c);
}

$ cc -o t1 t1.c

$ a.out
Floating exception(coredump)
$

Sponsored Links
    #2  
Old 10-30-2001
shaik786
Guest
 
Posts: n/a
hi!

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?

Rgds
SHAIK
Sponsored Links
    #3  
Old 10-30-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.

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

int c=a;

for(i=0;i<b-1;i++){
c*=a;
}
printf("%d",c);
}

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 10:20 AM
Return a value from called function to the calling function mvictorvijayan Shell Programming and Scripting 1 09-14-2009 04:19 AM
Passing global variable to a function which is called by another function sars Shell Programming and Scripting 4 06-30-2008 11:39 AM
Need help in using power function in Bash ahjiefreak Shell Programming and Scripting 3 03-26-2008 04:31 AM



All times are GMT -4. The time now is 02:17 AM.