The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Passing global variable to a function which is called by another function sars Shell Programming and Scripting 4 06-30-2008 08:39 AM
How to tell Power 4/5/6 thomn8r AIX 3 05-02-2008 01:00 PM
Need help in using power function in Bash ahjiefreak Shell Programming and Scripting 3 03-26-2008 01:31 AM
Failed to power up fredginting SUN Solaris 2 01-07-2008 08:05 PM
No Power Struggles: Coordinated Multilevel Power Management for the Data Center iBot UNIX and Linux RSS News 0 12-24-2007 02:20 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 10-30-2001
Registered User
 

Join Date: Aug 2001
Posts: 60
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)
$

Reply With Quote
Forum Sponsor
  #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
Reply With Quote
  #3  
Old 10-30-2001
Registered User
 

Join Date: Oct 2001
Location: San Jose
Posts: 5
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)
__________________
is there such a thing as a unix guru?
Reply With Quote
Google The UNIX and Linux Forums
Reply

« dup() | Semaphores »
Thread Tools
Display Modes




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


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0