General Programming Q - Is this bad?

Is declaring a variable in a loop bad? I'd imagine it is bad practice, but does it have implications on the performance of the program? Every time you re-define a variable does it get assigned a new memory address and would this slow down the program? Haha as you can see I don't really know much about how the computer works lol I never really thought about this stuff when I was programming in higher level languages!

1
2
3
4
5
6
7
8
9
10

for(int i=0; i<10; i++)
{

   float q;
   q = i*0.25;
   cout << q;

}


Also, might as well put this in the same thread - could someone please explain the return statement of the following code to me. I've never really seen the syntax before - 8 bits to a byte right? And on most computers an int is 4 bytes.... a char is 1 byte? Haha I don't know:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

unsigned int ReadInt (void)
{
 unsigned int temp = ReadChar();
 return ( temp | (ReadChar () << 8));    WHAT IS THIS?
}

.......................................

unsigned char ReadChar (void)
{
 return (fgetc (bin3ds));

}



Thanks, Nick
Last edited on
1) It's not bad and it shouldn't slow down the program
2) See http://www.cplusplus.com/doc/tutorial/operators/ ( scroll down to Bitwise Operators )
and http://www.cplusplus.com/doc/tutorial/variables/ for common type sizes
Topic archived. No new replies allowed.