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

[亡魂溺海] 提交于 2019-11-28 06:54:54

问题


I am writing a program for my intro to C class and keep getting some warnings when I try to compile with gcc.

Here is my code:

int main ()
{
float balance;
float beg_balance;
float withdrawal_amt;
float deposit_amt;
float amount;
int total_withdrawals;
int total_deposits;
int selection;

print_greeting ();

printf("Let's begin with your beginning balance");
beg_balance = get_positive_value();
do
{
print_menu ();
scanf("%d", &selection);

switch (selection)
  {
  case WITHDRAWAL:
    get_positive_value();
    balance = withdrawal(balance, withdrawal_amt, amount);
    break;
  case DEPOSIT:
    get_positive_value();
    balance = deposit(balance, deposit_amt, amount);
    break;
  case SUMMARY:
print_receipt(total_withdrawals, total_deposits, beg_balance, balance, \
withdrawal_amt, deposit_amt);
    break;
  case QUIT:
    break;
  default: printf("Invalid selection");
  break;
  }
}
while(selection != 4);

return 0;

The errors I am getting when compiling is this:

project.c: In function ‘main’:
project.c:46: warning: ‘withdrawal_amt’ may be used uninitialized in this function
project.c:46: warning: ‘amount’ may be used uninitialized in this function
project.c:50: warning: ‘deposit_amt’ may be used uninitialized in this function
project.c:53: warning: ‘total_withdrawals’ may be used uninitialized in this function
project.c:53: warning: ‘total_deposits’ may be used uninitialized in this function

Any ideas why? Thank you

EDIT:

Now i am having trouble creating a register function that is used to print out the transaction history of the account. It should print out the beginning and ending balances, as well as a table that shows all transactions (Deposits and Withdraws) that have occurred. Any help would be greatly appreciated


回答1:


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;



回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/20526053/c-warning-withdrawal-amt-may-be-used-uninitialized-in-this-function

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