1
  float f2,f1 = 123.125;

それらの違いは何ですか?

  float f1 = 123.125,f2;

コードを次のように書くと

float f1,f2 = 123.125

プログラムの結果は異なります

ここに完全なプログラムがあります

   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); 
4

2 に答える 2

4
  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

両方の変数を初期化したい場合は、行う必要があります

  float f1 = 123.125f, f2 = 123.125f;  

できればこのように書きます(読みやすくするため)

  float f1 = 123.125f;
  float f2 = 123.125f;  

「f」サフィックスに注意してください。これは、それが double 値ではなく float 値であることを示しています。

定義することもできます

#define INITVALUE 123.125f

float f1 = INITVALUE;
float f2 = INITVALUE;
于 2012-07-20T07:27:06.673 に答える
0

2 つの変数を同じ値で初期化する場合は、次の構文を使用します。

float f2 = f1 = 123.125f;

使用しているコードは、変数の 1 つを初期化しており、他の変数を初期化していないため、表示されている動作です。

于 2012-07-20T07:10:23.643 に答える