C 函数调用作为测试条件

发布于 2024-11-05 02:51:45 字数 1662 浏览 1 评论 0原文

几天前,我想到了一个奇怪的想法,即以一种奇怪的方式操作 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如果没结果 2024-11-12 02:51:45

对我来说,我认为这段代码将始终被评估为 true(我的假设),因为测试条件被函数调用替换。

这是不正确的。该函数(在本例中为 printf)返回一个值(在本例中为 int)。当您将其用作 if 语句中的条件时,将调用该函数,并且它返回的值将成为条件:如果返回零,则计算结果为 false;如果返回零,则计算结果为 false。如果它返回非零,则计算结果为 true。

之间没有区别

if (printf("Hello, World!")) { }

int i;
i = printf("Hello, World!");
if (i) { }

(当然,除了第二个示例中的附加变量之外)。


在修改后的第二个示例中,每次检查循环条件时都会调用 scanf 。您可以像这样重写循环:

while (1)
{
    int result_of_scanf;

    result_of_scanf = scanf("%d", &size);
    if (result_of_scanf == 0)
        break;

    if (count++ >= size)
        break;

    sum += count;
}

scanf 不仅仅被调用一次;而是被调用一次。循环的每次迭代都会调用它。 scanf 返回成功读取的元素数量,因此在本例中,它将返回 1(如果您输入 int< 范围内的有效整数) /code>) 或 0 (如果您提供任何其他输入)。

To me I think this code will always evaluated to be true(my assumption) since the test condition is substituted with a function call.

This is incorrect. The function (in this case, printf) returns a value (in this case, an int). When you use it as the condition in the if 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

if (printf("Hello, World!")) { }

and

int i;
i = printf("Hello, World!");
if (i) { }

(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:

while (1)
{
    int result_of_scanf;

    result_of_scanf = scanf("%d", &size);
    if (result_of_scanf == 0)
        break;

    if (count++ >= size)
        break;

    sum += count;
}

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 either 1 (if you input a valid integer within the range of int) or 0 (if you give any other input).

雪落纷纷 2024-11-12 02:51:45

该程序似乎卡住了,但实际上它正在等待您输入数字。也就是说,您必须输入数字,直到计数等于输入数字。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文