中級 › error: assignment of read-only variable の原因と直し方 › パターン3
error: assignment of read-only variable の原因と直し方
constをつけて宣言した変数に、後から値を代入しようとしたときに出るエラーです。
1 #include <stdio.h> 2 3 int main(void) { 4 const double rate = 0.05; 5 __________________ = 0.1; ^ 6 printf("%.2f\n", rate); 7 return 0; 8 }
main.c:5:10: error: assignment of read-only variable 'rate'
空欄を埋めてコンパイル・実行を通す
double型のconst変数でも同様です。一度決めた値を変更したくない場合にconstは効果を発揮します。
次の問題