C: warning: ‘withdrawal_amt’ may be used uninitialized in this function

十年热恋 提交于 2019-11-29 13:05:45
float balance;
float beg_balance;
float withdrawal_amt;
float deposit_amt;

You never attribute them any value. it's like if you wrote :

case DEPOSIT:
get_positive_value();
balance = deposit(balance, (float), amount);
break;

You need to init them like :

float withdrawal_amt = 0.0;

The errors you are getting are not errors but warnings. They point out that you do not initialize any of your automatic storage variables, so they will start up with an unspecified value.

You can initialize your variables, say to 0, and the warnings will disappear.

I think you want to use your function get_positive_value() like this:

withdrawal_amt = get_positive_value();

and similarly others.

You are passing withdrawal_amt, amount and other variables mentioned in warnings unintialized.

Note that all the variables declared inside some function are stored in some random memory (stack memory) location which compiler chooses, and that location may contain some garbage value which will be taken as initial value of your variables.

Hence compiler instructs you beforehand to initialize them to some known value, so that you do not get your bank balance -1000.00 USD when you 'deposited' 1000.00 USD ;-)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!