C 函数调用作为测试条件
几天前,我想到了一个奇怪的想法,即以一种奇怪的方式操作 if();
语句。让我们继续看一个简单的代码。
代码:
if(printf("blahblah\n");{
}
我的想法:
1.)对我来说,我认为这段代码将始终被评估为 true(我的假设),因为测试条件被函数调用替换。
所以今天我正在做一本书提供的练习(只是为了帮助我复习几天前学到的知识)。这就是代码。
代码:
#include <stdio.h>
int main(void) // This program would add up the value enter by user , for e.g with the
{ //input of 20 , it will print out the sum of 1+2+3+4+5.....20.
int count , sum , size;
count = 0;
sum = 0;
printf("Enter a value to find the sum of it from 1 : ");
scanf("%d" , &size);
while (count++ < size)
sum = sum + count;
printf("sum = %d\n" , sum);
return 0;
}
通过在第一个代码上使用我的想法,我将第二个代码修改为这个。
#include <stdio.h>
int main(void)
{
int count , sum , size;
count = 0;
sum = 0;
printf("Enter a value to find the sum of it from 1 : ");
while (scanf("%d" , &size) && count++ < size )
sum = sum + count;
printf("sum = %d\n" , sum);
return 0;
}
问题:
1.)基于我在第一个代码中所做的假设code 中, scanf()
函数假定始终评估为 true。这就是为什么第二个测试条件 count++
count++
size
是决定 while 语句中的语句是否执行的参数。
2.)但是当我运行程序时,我输入 30 但它不起作用,程序只是停在那里,在我按回车键后不执行任何操作。
3.)我尝试使用 `count++ < 来切换测试条件大小作为左操作数,而输入函数作为右操作数。
4.)这样做之后,我得到的结果是不同的。当我尝试运行程序时,程序执行第二个printf()
函数语句,并打印出sum = 0< /代码>。
非常感谢您的帮助,请纠正我的错误。我愿意从中学习。
Few days ago I've a weird idea came into my mind that manipulate if();
statement in a weird way.Let's go on to a simple code.
The Code :
if(printf("blahblah\n");{
}
My Idea :
1.)To me I think this code will always evaluated to be true(my assumption) since the test condition is substituted with a function call.
So today I'm doing an exercise provided by a book(just to help me refresh what i learn few days ago).This is the code.
The Code :
#include <stdio.h>
int main(void) // This program would add up the value enter by user , for e.g with the
{ //input of 20 , it will print out the sum of 1+2+3+4+5.....20.
int count , sum , size;
count = 0;
sum = 0;
printf("Enter a value to find the sum of it from 1 : ");
scanf("%d" , &size);
while (count++ < size)
sum = sum + count;
printf("sum = %d\n" , sum);
return 0;
}
By using my idea on the first code , I modified the second code into this.
#include <stdio.h>
int main(void)
{
int count , sum , size;
count = 0;
sum = 0;
printf("Enter a value to find the sum of it from 1 : ");
while (scanf("%d" , &size) && count++ < size )
sum = sum + count;
printf("sum = %d\n" , sum);
return 0;
}
The Question :
1.)Based on the assumption made by me in the first code , the scanf()
function suppose to be always evaluated to true.That's why the second test condition count++ < size
is the one that determine whether the statement in while
statement will be executed or not.
2.)But when I run the program , I input 30 but it doesn't work , the program just stop there without doing anything after i hit enter.
3.)I try to switch the to test condition with the `count++ < size as left operand while the input function as right operand.
4.)After doing so , the result i get is different.When i try to run the program , the program execute the second printf()
function statement , and print out sum = 0
.
Your help is much appreciated , do correct me for mistakes.I'm willing to learn from it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是不正确的。该函数(在本例中为
printf
)返回一个值(在本例中为int
)。当您将其用作if
语句中的条件时,将调用该函数,并且它返回的值将成为条件:如果返回零,则计算结果为 false;如果返回零,则计算结果为 false。如果它返回非零,则计算结果为 true。之间没有区别
和
(当然,除了第二个示例中的附加变量之外)。
在修改后的第二个示例中,每次检查循环条件时都会调用
scanf
。您可以像这样重写循环:scanf
不仅仅被调用一次;而是被调用一次。循环的每次迭代都会调用它。scanf
返回成功读取的元素数量,因此在本例中,它将返回1
(如果您输入int< 范围内的有效整数) /code>) 或
0
(如果您提供任何其他输入)。This is incorrect. The function (in this case,
printf
) returns a value (in this case, anint
). When you use it as the condition in theif
statement, the function is called and the value it returns becomes the condition: if it returns zero it evaluates to false; if it returns nonzero it evaluates to true.There is no difference between
and
(aside, of course, from the additional variable in the second example.)
In your modified second example,
scanf
is called each time the loop condition is checked. You could rewrite the loop like this:scanf
doesn't just get called once; it gets called for each iteration of the loop.scanf
returns the number of elements that it read successfully, so in this case it will return either1
(if you input a valid integer within the range ofint
) or0
(if you give any other input).该程序似乎卡住了,但实际上它正在等待您输入数字。也就是说,您必须输入数字,直到计数等于输入数字。
The program appears to be stuck but actually it is expecting you to input numbers. That is you will have to enter numbers till the count is equal to the input number.