Note that there are some explanatory texts on larger screens.

plurals
  1. POC function call as test condition
    primarykey
    data
    text
    <p><i>Few days ago I've a weird idea came into my mind that manipulate <code>if();</code> statement in a weird way.Let's go on to a simple code.</i></p> <p><b>The Code : </b></p> <pre><code>if(printf("blahblah\n");{ } </code></pre> <p><br /></p> <p><b>My Idea :</b></p> <p>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.</p> <p><br /> <br /> <i>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.</i></p> <p><b>The Code :</b></p> <pre><code>#include &lt;stdio.h&gt; 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" , &amp;size); while (count++ &lt; size) sum = sum + count; printf("sum = %d\n" , sum); return 0; } </code></pre> <p><br /> <br /></p> <p><i>By using my idea on the first code , I modified the second code into this.</i></p> <pre><code>#include &lt;stdio.h&gt; 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" , &amp;size) &amp;&amp; count++ &lt; size ) sum = sum + count; printf("sum = %d\n" , sum); return 0; } </code></pre> <p><b>The Question :</b></p> <p>1.)Based on the assumption made by me in the first code , the <code>scanf()</code> function suppose to be always evaluated to true.That's why the second test condition <code>count++ &lt; size</code> is the one that determine whether the statement in <code>while</code> statement will be executed or not.</p> <p>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.</p> <p>3.)I try to switch the to test condition with the `count++ &lt; size as left operand while the input function as right operand.</p> <p>4.)After doing so , the result i get is different.When i try to run the program , the program execute the second <code>printf()</code> function statement , and print out <code>sum = 0</code>.</p> <p>Your help is much appreciated , do correct me for mistakes.I'm willing to learn from it. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload