Take the 2-minute tour ×

Can anyone help with the following code?
New to c from python so I know my code is wrong. I trying to make a code that can round down a float and multiply a char by said float

float avg;
char star='*';
float top;
float bottom;



output needs to be

* * * * * *   

what I'm getting is


I've also tried to use floor to round the avg but that just turns the spade into a different character and 6.25 becomes 6.00000000000000

share|improve this question
Did you expect star *= avg; to produce "* * * * * *"? That's not how it works, C is not Python. –  Daniel Fischer Mar 24 '13 at 1:00
I've only ever used python before so I have no idea how to do this in c. –  user2054056 Mar 24 '13 at 1:03
Either you make floor(avg) calls to printf that each print one '*', or you create a char stars[(int)avg + 1]; and fill that with '*', and a 0 to terminate it. –  Daniel Fischer Mar 24 '13 at 1:05
add comment

2 Answers

up vote 1 down vote accepted

You should use a loop to display your *s;

for( int i = 0; i < avg; ++ i ) {
    printf( "* " );
printf( "\n" );

(get rid of the star variable).

To explain the behaviour you saw: when you do star *= avg;, what actually happens is the ascii value of star, which is * => 42, gets multiplied by avg, which is 6.25. This produces 262.5, which is shortened (modulo 256) to 6.5 and the decimal is removed (6), to fit in a char again. 6 in ascii isn't usually a visible character (technically it's "acknowledge"), but in the codepage you're using, it turns out it's drawn as a spade symbol.

share|improve this answer
add comment

What you need is an for loop:


In C, when you multiply an char and an float you got another float (your first float multiplied by the ascii code of the char), because you are printing like an char printf will cast the float to char and print the corresponding char.

share|improve this answer
add comment

Your Answer


