Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4564338
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T18:29:12+00:00 2026-05-21T18:29:12+00:00

Few days ago I’ve a weird idea came into my mind that manipulate if();

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T18:29:13+00:00Added an answer on May 21, 2026 at 6:29 pm

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A few days ago I asked a question that might be a little unclear.
I lost my temper few days ago because of this simple problem. Can you
Few days ago I tried to create a system that jQuery will enable me
few days ago, my freind asked me to build him a proagram that start
A few days ago I posted this question: switch statement and loops using jquery/javascript
A few days ago I was told that recent versions of g++ produce better
Few days ago, I came to a problem where I have to sum the
A few days ago I've discovered that singleton can become anti-pattern in Android. My
A few days ago I saw that for ( ; ; ) results in
Few days ago I found out that my webapp wrote ontop of the tornadoweb

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.