Reputation: 751
float f2,f1 = 123.125;
what is different between them?
float f1 = 123.125,f2;
if I write code as
float f1,f2 = 123.125
the program will has different result
here is the full program
float f2,f1 = 123.125;
//float f1 = 123.125,f2;
int i1,i2 = -150;
i1 = f1; //floating to integer
NSLog(@"%f assigned to an int produces %i",f1,i1);
f1 = i2; //integer to floating
NSLog(@"%i assigned to a float produces %f",i2,f1);
f1 = i2/100; //integer divided by integer
NSLog(@"%i divied by 100 prouces %f",i2,f1);
f2= i2/100.0; //integer divided by a float
NSLog(@"%i divied by 100.0 produces %f",i2,f2);
f2= (float)i2 /100; // type cast operator
NSLog(@"(float))%i divided by 100 produces %f",i2,f2);
Upvotes: 1
Views: 157
Reputation: 36082
float f2,f1 = 123.125; // here you leave f2 uninitialized and f1 is initialized
float f1 = 123.125,f2; // here you leave f2 uninitialized and f1 is initialized
float f1,f2 = 123.125; // here you leave f1 uninitialized and f2 is initialized
if you want to initialize both variables you need to do
float f1 = 123.125f, f2 = 123.125f;
preferably you write like this (for readability)
float f1 = 123.125f;
float f2 = 123.125f;
note the "f" suffix, it indicates that it is a float value and not a double.
you can also do a define
#define INITVALUE 123.125f
float f1 = INITVALUE;
float f2 = INITVALUE;
Upvotes: 4
Reputation: 122381
If you want to initialise two variables with the same value then use this syntax:
float f2 = f1 = 123.125f;
The code you are using is initialising one of the variables and not the other, hence the behaviour you are seeing.
Upvotes: 0