C function call as test condition
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.
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).
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.
精彩评论