[英]Declaring variables outside loop/IF structures in C
I'm new to the C language, rather programming overall.我是 C 语言的新手,而不是整体编程。 I was wondering why is it that when I declare a variable to be used within an if statement OUTSIDE the structure, that the output I've received is incorrect (for this piece of code anyway).
我想知道为什么当我在结构外的 if 语句中声明要使用的变量时,我收到的输出不正确(无论如何对于这段代码)。
Here's my code:这是我的代码:
#include<stdio.h>
void grossPay();
int main()
{
grossPay();
}
void grossPay()
{
int rate = 10, hours;
double tax, grosspay, netpay;
printf("Enter work hours this week: ");
scanf("%d", &hours);
grosspay = hours * rate;
if (grosspay <= 300 && grosspay > 0)
{
tax = 0.10;
netpay = grosspay - grosspay * tax;
printf("Pay for %d hours of week with $%d per hour\n", hours, rate);
printf("Gross pay: $%.2f\n", grosspay);
printf("Net pay: $%.2f\n", netpay);
}
else if (grosspay > 300 && grosspay <=1000)
{
tax = 0.15;
netpay = grosspay - grosspay * tax;
printf("Pay for %d hours of week with $%d per hour\n", hours, rate);
printf("Gross pay: $%.2f\n", grosspay);
printf("Net pay: $%.2f\n", netpay);
}
else if (grosspay > 1000)
{
tax = 0.25;
netpay = grosspay - grosspay * tax;
printf("Pay for %d hours of week with $%d per hour\n", hours, rate);
printf("Gross pay: $%.2f\n", grosspay);
printf("Net pay: $%.2f\n", netpay);
}
else
{
printf("Invalid input. Please try again.\n\n");
}
}
Edit: The code I placed was my 'fix' for not getting the correct output.编辑:我放置的代码是我没有得到正确输出的“修复”。 I expected that when I declared the netpay variable once outside the entire IF statement that I would receive the correct output, the same output from the code above.
我期望当我在整个 IF 语句之外声明 netpay 变量时,我会收到正确的输出,与上面代码的输出相同。
Edit 2: Previous version编辑 2:以前的版本
#include<stdio.h>
void grossPay();
int main()
{
grossPay();
}
void grossPay()
{
int rate = 10, hours;
double tax, grosspay, netpay;
printf("Enter work hours this week: ");
scanf("%d", &hours);
grosspay = hours * rate;
netpay = grosspay - grosspay * tax;
if (grosspay <= 300 && grosspay > 0)
{
tax = 0.10;
printf("Pay for %d hours of week with $%d per hour\n", hours, rate);
printf("Gross pay: $%.2f\n", grosspay);
printf("Net pay: $%.2f\n", netpay);
}
else if (grosspay > 300 && grosspay <=1000)
{
tax = 0.15;
printf("Pay for %d hours of week with $%d per hour\n", hours, rate);
printf("Gross pay: $%.2f\n", grosspay);
printf("Net pay: $%.2f\n", netpay);
}
else if (grosspay > 1000)
{
tax = 0.25;
printf("Pay for %d hours of week with $%d per hour\n", hours, rate);
printf("Gross pay: $%.2f\n", grosspay);
printf("Net pay: $%.2f\n", netpay);
}
else
{
printf("Invalid input. Please try again.\n\n");
}
}
The relevant code boils down to:相关代码归结为:
double tax;
netpay = grosspay - grosspay * tax;
tax = 0.10;
printf("Net pay: $%.2f\n", netpay);
The problem with this is that statements in a program are executed in the order you've written them (at least within a function, barring special control flow statements such as continue
or goto
).这样做的问题是程序中的语句是按照您编写它们的顺序执行的(至少在函数中,除非特殊的控制流语句,例如
continue
或goto
)。
Thus:因此:
tax
, which is uninitialized.tax
的局部变量,它是未初始化的。netpay
to the result of grosspay - grosspay * tax
.netpay
设置为grosspay - grosspay * tax
的结果。 This is already wrong because tax
has no defined value at this point, so grosspay - grosspay * tax
produces undefined results.tax
已经在这一点上没有定义的值,所以grosspay - grosspay * tax
将产生不确定的结果。tax
.tax
。 This has no effect on the value of netpay
.netpay
的价值没有影响。netpay
.netpay
。 Things are happening in the wrong order.事情以错误的顺序发生。 You need to set variables before you use them.
您需要在使用变量之前设置它们。
It's like you're telling someone:就像你在告诉某人:
And you're wondering why they're not reading from The Lord of the Rings.你想知道为什么他们不读指环王。
Maybe you are trying to eliminate the duplicate code.也许您正试图消除重复代码。 Since the difference between the
if
blocks is the tax rate, you could set the rate and at the end make one calculation.由于
if
块之间的差异是税率,您可以设置税率并在最后进行一次计算。
#include<stdio.h>
void grossPay();
int main()
{
grossPay();
}
void grossPay()
{
int rate = 10, hours;
double tax, grosspay, netpay;
printf("Enter work hours this week: ");
scanf("%d", &hours);
grosspay = hours * rate;
if (grosspay <= 300 && grosspay > 0)
{
tax = 0.10;
}
else if (grosspay > 300 && grosspay <=1000)
{
tax = 0.15;
}
else if (grosspay > 1000)
{
tax = 0.25;
}
else
{
printf("Invalid input. Please try again.\n\n");
return;
}
netpay = grosspay - grosspay * tax;
printf("Pay for %d hours of week with $%d per hour\n", hours, rate);
printf("Gross pay: $%.2f\n", grosspay);
printf("Net pay: $%.2f\n", netpay);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.