Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

cybrscot

macrumors 6502
Original poster
Dec 7, 2010
282
0
Somewhere in Southeast Asia
I'm not sure what's wrong here, obviously it all looks good to me, but it isn't working. Any help? The compiler errors are posted below the code

#include <stdio.h>

main ( )
{

int r = 10 ;
float _π = 3.14 ;
float volume = 4.0/3.0*π*r^3 ;

printf ("Volume of a sphere: %.2f\n" , volume) ;

return 0 ;
}

Scott-Deans-MacBook-Pro:documents scottdean$ gcc ~/documents/sphere.c
/Users/scottdean/documents/sphere.c: In function ‘main’:
/Users/scottdean/documents/sphere.c:7: error: stray ‘\317’ in program
/Users/scottdean/documents/sphere.c:7: error: stray ‘\200’ in program
/Users/scottdean/documents/sphere.c:8: error: stray ‘\317’ in program
/Users/scottdean/documents/sphere.c:8: error: stray ‘\200’ in program
/Users/scottdean/documents/sphere.c:8: error: invalid type argument of ‘unary *’
 
It look like you may have written this in an editor that doesn't stick to low ASCII. Try saving as plain text or using another editor that allows that.

^ is not exponentiation, it is binary XOR.

Also, there is no n, there is _n. I wouldn't name things with a leading underscore, this is generally "reserved".

-Lee
 
I'm not sure what's wrong here, obviously it all looks good to me, but it isn't working. Any help? The compiler errors are posted below the code

#include <stdio.h>

main ( )
{

int r = 10 ;
float _π = 3.14 ;
float volume = 4.0/3.0*π*r^3 ;

printf ("Volume of a sphere: %.2f\n" , volume) ;

return 0 ;
}

Scott-Deans-MacBook-Pro:documents scottdean$ gcc ~/documents/sphere.c
/Users/scottdean/documents/sphere.c: In function ‘main’:
/Users/scottdean/documents/sphere.c:7: error: stray ‘\317’ in program
/Users/scottdean/documents/sphere.c:7: error: stray ‘\200’ in program
/Users/scottdean/documents/sphere.c:8: error: stray ‘\317’ in program
/Users/scottdean/documents/sphere.c:8: error: stray ‘\200’ in program
/Users/scottdean/documents/sphere.c:8: error: invalid type argument of ‘unary *’

It looks like you got some strange characters in your source code. How did you enter this code into XCode?
 
Thanks for helping,

I'm using textwrangler for my editor, which is supposed to be pretty good.

I had to search the web to find out how to type an exponent, "they" said that ^ was exponent So I typed r^3

Also, I needed to type PI, or 3.14, the web said that Alt P, was PI, which it does look like the PI symbol in my editor, but when I copy/paste here it looks like the letter n. I tried to type it here π, but as you can see, it still looks like the letter n, not the PI symbol.
 
I just replaced the PI symbol with the letter p instead as a substitute, it changed my compiler errors, seems to be less. Here is the new code and the errors.

#include <stdio.h>

main ( )
{

int r = 10 ;
float p = 3.14 ;
float volume = 4.0/3.0*p*r^3 ;

printf ("Volume of a sphere: %.2f\n" , volume) ;

return 0 ;
}



Scott-Deans-MacBook-Pro:documents scottdean$ gcc ~/documents/sphere.c
/Users/scottdean/documents/sphere.c: In function ‘main’:
/Users/scottdean/documents/sphere.c:8: error: invalid operands to binary ^ (have ‘double’ and ‘int’)
 
Thanks for helping,

I'm using textwrangler for my editor, which is supposed to be pretty good.

I had to search the web to find out how to type an exponent, "they" said that ^ was exponent So I typed r^3

Also, I needed to type PI, or 3.14, the web said that Alt P, was PI, which it does look like the PI symbol in my editor, but when I copy/paste here it looks like the letter n. I tried to type it here π, but as you can see, it still looks like the letter n, not the PI symbol.

Don't try to use anything fancy. Name the variable pi, don't try to use the symbol for pi. It is illegal in C source and has no special meaning.

^ may be exponentiation in some languages, but not C. It is a binary operator, but won't do what you want at all.

-Lee
 
I just replaced the PI symbol with the letter p instead as a substitute, it changed my compiler errors, seems to be less. Here is the new code and the errors.

#include <stdio.h>

main ( )
{

int r = 10 ;
float p = 3.14 ;
float volume = 4.0/3.0*p*r^3 ;

printf ("Volume of a sphere: %.2f\n" , volume) ;

return 0 ;
}



Scott-Deans-MacBook-Pro:documents scottdean$ gcc ~/documents/sphere.c
/Users/scottdean/documents/sphere.c: In function ‘main’:
/Users/scottdean/documents/sphere.c:8: error: invalid operands to binary ^ (have ‘double’ and ‘int’)

Change r^3 to r*r*r. ^ is not exponentiation. The left operand in this case will be a double, ^ only works on integer types. And it's not exponentiation.

-Lee
 
everything worked good! The PI symbol and the ^ were the problems,

"adding to my knowledge everyday"

This is fun, but it's especially rewarding when after getting stuck on a problem, I finally get it to work, and they're just some small problems that I understand. I'm happy there wasn't something inherently wrong with the way I wrote my code. A couple rogue symbols I can deal with.
 
This may sound like a stupid question, not that the others didn't for you pro's, but does the computer know the correct order of operations?

They don't know anything but what we tell them to do.

There is an order of operator precedence for C.
http://www.difranco.net/cop2220/op-prec.htm

That lists the precedence and associativity for all of the operators. When in doubt, parenthesize.

-Lee
 
This may sound like a stupid question, not that the others didn't for you pro's, but does the computer know the correct order of operations?

The computer knows nothing. The C compiler knows the correct order of operations according to the rules of the C Standard. And you will have to learn the same rules. I think you really need to get a good C book.
 
I've got a very good C book. C Programming by K.N. King. Was highly recommended by many. But I'm just doing the exercises at the end of chapter 1. Ch. 1 didn't discuss exponents or PI symbols. So I searched on the web how to type them with the keyboard.

When all that fails I have to ask you guys. I have no teacher, teaching myself. Here in Vietnam, there are no courses to learn programming, or I'd gladly enroll even if they don't speak English!

Trust me, I appreciate all your help, and I only ask after I can't find the answer searching Google and reading 3-4 different posts that lead me nowhere. I have spent a couple hours on this problem alone, had no idea C couldn't read those characters, just went to learn how to make them with the keyboard and did it. I didn't think the character would matter, whether I used any letter for a variable, or a symbol, whatever, as long as I defined the variable. But now I know!!
:)
 
For exponentiation, there are a couple of options.

#1, <math.h> has a function
double pow(double x, double y);
Which raises x to the power y

This is known to be slower for whole powers like pow(2.0, 3.0);
So if you are only usuing whole powers I'd suggest you make your own function like this
#2,
Code:
// pre: y>=0
double myIntPow(double x, int y) {
    double temp = 1.0;
    for ( ; y > 0; y--) {
        temp *= x;
    }
    return temp;
}
Oh and <math.h> has a constant M_PI which is Pi in as much precision as needed for a double (presumably). (has a lot of other M_something for lots of other constants)
 
Ch. 1 didn't discuss exponents or PI symbols.
That's probably because C does not have an exponentiation operator or a π symbol, although <math.h> does have a pow function.
Some versions of <math.h> may also have a M_PI macro, but this is non-standard, so you should not depend on it.

Code:
double myIntPow(double x, int y) {
    double temp = 1.0;
    for ( ; y > 0; y--) {
        temp *= x;
    }
    return temp;
}

If y can be large, it may be more efficient to do it more like:
Code:
    if( y<0 ){ y = -y; x = 1.0/x; }
    for( ; y > 0; y>>=1 ){
        if( y&1 ){ temp *= x; }
        x *= x;
    }
 
Use the pow() function and be happy. Libraries are well-tested and generally will perform better than something you can write yourself. Some compilers even recognize calls to the standard library and inline them with very optimized versions.

Only write your own version if you've run your software through a profiler and identified the power function as the most important performance problem and you have time to test it thoroughly for correctness.
 
Use the pow() function and be happy. Libraries are well-tested and generally will perform better than something you can write yourself. Some compilers even recognize calls to the standard library and inline them with very optimized versions.

Only write your own version if you've run your software through a profiler and identified the power function as the most important performance problem and you have time to test it thoroughly for correctness.

I once believed that too, however someone once raised the issue with pow based on the method pow uses to solve (mainly that it doesn't require the power to be a whole number so it uses a somewhat convoluted way of solving)

So one time I wrote a test program to test it out (in C obviously). If memory serves me, it was similar to x*x for pow(x, 2.0) probably because of some internal check for 2.0 and just the function call overhead. However x*x*x was significantly faster than pow(x, 3.0) and it got worse from there.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.