C语言未初始化变量警告求助:运行触发-Wuninitialized警告
Hey there! Let's walk through why you're seeing those -Wuninitialized warnings and fix the underlying problems in your code—they're tied to how C handles function parameter passing, which is the root cause here.
Why the Warnings Happen
You declared product, money, and change in main() but didn't initialize them:
int product, money, change;
Worse, even though you call GetData(money, product) to set their values, C uses pass-by-value by default. That means GetData is working with copies of your main() variables, not the originals. So after the function call, your main() variables are still uninitialized—hence the compiler warnings. The same logic applies to CalculateChange modifying change and the coin count variables.
Step-by-Step Fixes
1. Use Pointer Parameters to Modify Original Variables
Change your functions to accept pointers instead of regular integers, so they can directly update the variables in main().
Updated GetData Function
void GetData (int *money, int *product) { // Asks user how much was the product and how much they paid the cashier. printf("\nHow much was the product?\n"); scanf("%d", product); // product is already a pointer, no need for & if((*product < 5) || (*product > 95) || (*product %5 != 0)) { printf("\nInvalid number!\nNumber has to be a multiple of 5 and cannot be less than 5 or more than 95.\n"); // Optional: Add logic to re-prompt for valid input here } else { printf("How much did you give to the cashier? \n"); scanf("%d", money); // money is a pointer, no need for & if (*money < *product) { printf("You did not give enough money to the cashier!"); // Optional: Add logic to handle insufficient payment here } } return; }
Updated CalculateChange Function
We'll also adjust this function to update the coin count variables from main() directly via pointers:
void CalculateChange (int money, int product, int *fifty, int *twenty, int *ten, int *five) { // Calculates the amount of change needed. int change = money - product; *fifty = change / 50; change %= 50; *twenty = change / 20; change %= 20; *ten = change / 10; change %= 10; *five = change / 5; change %= 5; return; }
2. Update Function Calls in main()
Pass the addresses of your variables to the functions (using &) so the pointers can reference the originals:
GetData (&money, &product); CalculateChange (money, product, &fifty, &twenty, &ten, &five);
3. Initialize Variables (Optional but Safe)
To fully eliminate any lingering warnings, initialize your variables when declaring them in main():
int product = 0, money = 0, change = 0;
4. Fix the scanf for User Response
Your current scanf("%c", &response) will read the leftover newline character from previous scanf calls. Add a space before %c to skip whitespace:
scanf(" %c", &response );
Full Fixed Code
#include <stdio.h> #include <ctype.h> void GetData (int *money, int *product) { // Asks user how much was the product and how much they paid the cashier. printf("\nHow much was the product?\n"); scanf("%d", product); if((*product < 5) || (*product > 95) || (*product %5 != 0)) { printf("\nInvalid number!\nNumber has to be a multiple of 5 and cannot be less than 5 or more than 95.\n"); } else { printf("How much did you give to the cashier? \n"); scanf("%d", money); if (*money < *product) { printf("You did not give enough money to the cashier!"); } } return; } void CalculateChange (int money, int product, int *fifty, int *twenty, int *ten, int *five) { // Calculates the amount of change needed. int change = money - product; *fifty = change / 50; change %= 50; *twenty = change / 20; change %= 20; *ten = change / 10; change %= 10; *five = change / 5; change %= 5; return; } void PrintResults (int fifty, int twenty, int ten, int five) { // Prints the amount of coins needed. printf("\nThe number of fifty cents: %d", fifty); printf("\nThe number of twenty cents: %d", twenty); printf("\nThe number of ten cents: %d", ten); printf("\nThe number of five cents: %d", five); return; } int main () { int product = 0, money = 0, change = 0; int fifty = 0; int twenty = 0; int ten = 0; int five = 0; char response; do { GetData (&money, &product); CalculateChange (money, product, &fifty, &twenty, &ten, &five); PrintResults (fifty, twenty, ten, five); printf("\nWould like to quit the program? Enter y or n\n"); scanf(" %c", &response ); response = tolower (response); printf("--------------------------------------------------------------------------"); } while (response != 'y'); printf("\nQuitting program...\n"); return (0); }
Bonus Notes
- You might want to add loop logic in
GetDatato re-prompt users for valid input (right now, invalid input just prints a message but proceeds with unvalidated values). - The
changevariable inmain()isn't used anymore, so you could remove it if you want.
内容的提问来源于stack exchange,提问作者user13206108

