![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
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) $ |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
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 |
|
#3
|
|||
|
|||
|
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? |
|||
| Google The UNIX and Linux Forums |