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 6096099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:53:15+00:00 2026-05-23T12:53:15+00:00

while ((x[num++] = getchar()) != ‘ ‘); This reads a char at a time

  • 0
while ((x[num++] = getchar()) != ' ');

This reads a char at a time and stops if it encounters a space.
But how does it evaluate the parenthesis with the space? What is the result of the parenthesis? So basically, what does this (x[num++] = getchar()) equal to, in order to compare it to a space?

I found this example in one of my class books, it was different and I got a warning so I guess it’s a not good practice is it? How does the evaluation go here? Does it first assign the read value to x[num] and then compares it to space?

while(x[num++] = getchar() != ' ');
  • 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-23T12:53:15+00:00Added an answer on May 23, 2026 at 12:53 pm

    Neither version is good coding; both ignore the possibility of reaching EOF before reading a space, and both ignore the possibility of overflowing the buffer before reading a space.

    The first is otherwise correct. It contains an assignment:

    x[num++] = getchar()
    

    The result of an assignment is the value assigned. So, if getchar() returns an ‘X’, the result of the assignment is ‘X’. Then the result of the assignment is compared with blank; they are different, and the loop repeats. If getchar() returns a blank, then the result of the assignment is also a blank, and blank equals blank, so the loop terminates.

    The second is plain faulty. Because assignment has a lower precedence than !=, the result is as if the code read:

    while (x[num++] = (getchar() != ' '))
        ;
    

    That is, a character is read by getchar() and compared with blank, generating a value 1 if the character is not a blank and 0 if it is a blank. This 0 or 1 is assigned to x[num++], and then evaluated as a logical condition. If the result was 0, the loop terminates; if the result was 1, the loop continues. Note that the characters read are not recorded in this version – which is why the compiler gives the warning. It is almost invariably a mistake; on those odd occasions when it is not a mistake, you can tell the compiler that by providing the extra parentheses to make your intention crystal clear to the compiler – and to your human audience, the other people who will read your code. (Remember: if you come back in 6 months, or even 6 weeks, you’ll be a different person and may well have difficulty remembering such subtleties. Make it plain to everyone. There is a fine balance to be drawn between over-parenthesizing and under-parenthesizing code.)

    The code should probably read:

    int c;
    int max = sizeof(x) - 1;
    int num = 0;
    
    while ((c = getchar()) != EOF && num < max && (x[num++] = c) != ' ')
        ;
    

    Note, in particular, that c must be an int and not a char.

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

Sidebar

Related Questions

No related questions found

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.