C++ プログラムの出力 |セット22

次の C++ プログラムの出力を予測します。 質問1 CPP
   #include          using     namespace     std  ;   int     main  ()   {      int     a     =     b     =     c     =     0  ;      cout      < <     a      < <     '*'      < <     b      < <     '*'      < <     c  ;      return     0  ;   }   
Output:
Compile time error!  
説明: A chained statement cannot be used to initialize variables at the time of declaration. Hence the statement a = b = c = 0; is an illegal statement. However following way a legal syntax and can be used in C++ or C.
int abc; a = b = c = 0;  

質問2 CPP
   #include          using     namespace     std  ;   int     main  ()   {      for     (     ;     ;     )     cout      < <     'blank'  ;      return     0  ;   }   
Output:
Infinite Loop!  
説明: for ループ内に初期化テスト条件とインクリメント/デクリメント条件が欠落しているため、実行は無限ループに陥ります。
質問3 CPP
   #include          using     namespace     std  ;   int     main  ()   {         int     i  ;      for     (  i  =  0  ;     i   <  3  ;     i  ++  );          cout      < <     'hello!'      < <  i  ;      return     0  ;   }   
Output:
hello!3  
説明: Hello!3 は、for ループが定義されている行にセミコロン (;) が存在するため、for ループの実行時に実行するタスクがないため、結果が Hello!3 になります。ループは 3 回実行され、i の値が 3 になり、残りのステートメントが実行されます。
質問4 CPP
   #include          using     namespace     std  ;   int     main  ()   {      int     i  ;      i     =     1     +     (  1    4    5    6    3  );      cout      < <     i  ;      return     0  ;   }   
Output:
4  
説明: カンマ () 演算子は、最初のオペランドを評価して結果を破棄し、次に 2 番目のオペランドを評価して 2 番目の値を返す二項演算子です。ここで、カンマ () 演算子の結合性は左から右であり、expression(14563) が 3 と評価され、結果 1 + 3 が i に割り当てられることが容易に理解できます。
質問5 CPP
   #include          using     namespace     std  ;   int     main  ()   {      int     a     =     0       b  ;      b     =     (  a     =     50  )     +     10  ;      cout      < <     a      < <     '$'      < <     b  ;      return     0  ;   }   
Output:
50  
説明: ステートメント b = (a = 50) + 10;埋め込み割り当ての概念を使用します。ここでは、値 50 が変数 a に代入され、結果の 50+10 が b に代入されます。 質問6 CPP
   #include       using     namespace     std  ;   int     main  ()   {      char     a     =     30       b     =     40       c     =     10  ;      char     d     =     (  a  *  b  )  /  c  ;      cout      < <     int  (  d  );      return     0  ;   }   
Output:
120  
説明: C++ では、char データ型の変数に対して算術演算を実行する場合、文字の ASCII 値を考慮して算術計算を実行することもできます。この場合の答えは、x の ASCII 値である 120 です。
質問7 CPP
   #include       using     namespace     std  ;   int     main  (  int     x  )   {      static     int     i     =     5  ;      if     (  --  i  )      {      cout      < <     i  ;      main  (  10  );      }      return     0  ;   }   
Output:
4321  
説明: C++ ではゼロ以外の数値はすべて true 値として扱われます。このコードでは、if ステートメントは i の値を減らす傾向がありますが、if のブロック内で main() 関数が何度も呼び出されます。ここでプログラムは無限ループにあるように見えますが、変数 i は本質的に静的であり、実行されるまで存続期間があるため、i が 0 になるとプログラムは停止します。
質問8 CPP
   #include       using     namespace     std  ;   int     main  (  int     x  )   {      int     i     =     5  ;      if     (  --  i  )      {      cout      < <     i  ;      main  (  10  );      }      return     0  ;   }   
Output:
infinite loop  
説明: C++ ではゼロ以外の数値はすべて true 値として扱われます。このコードでは、if ステートメントは i の値を減らす傾向がありますが、if のブロック内で main() 関数が何度も呼び出されます (すべてのステートメントが新しいプログラムであるかのように実行されます)。そのため、終了条件がないため、プログラムの実行は無限ループに巻き込まれます。 クイズの作成