Here's the question in the while statement.

Asked 1 years ago, Updated 1 years ago, 39 views

#include<stdio.h>

main()
{
  charc='\0';
  char prevletter;
  int wordnum;
  int word_in;

  while(1)
  {
    wordnum = 0;
    word_in=1;
    previewer='\0';
    printf("Please enter a string:");
    while(1)
    {
      c=getchar();
      if(c=='\n')
      {
          if(word_in)
            wordnum++;
          break;
      }
      previewer=c;
      if(c=='||c=='.')
      {
          if(word_in)
          {
            wordnum++;
            word_in=0;
          }
      }
      else
          word_in=1;
    }
    if(prevletter=='\0')
      break;
    printf("word count: %d\n", wordnum);
  }
}

Out of the above programs

  • What is 1 in line 10 and line 16 while(1)?(Error on 0 <2 passed successfully.)
  • Is line 13 word_in=1; a declaration for an infinite loop?
  • What does line 21 and line 28 if(word_in) mean?
    Please teach me.

c

2022-09-30 21:28

2 Answers

while(1) what is 1

while(cond) repeats while cond is non-zero, so any value other than zero can be an infinite loop, but while(1) can generally avoid unnecessary confusion as a snippet of infinite loops.

Is word_in=1; a declaration for an infinite loop?

I don't think so.It seems to be a variable to keep "Whether or not the last character is a word string (not a space or .)."

What does if(word_in) mean

In this case (line 21 and 28 are new lines and delimiting characters respectively) In that case, under the above conditions, the word count will be increased, so this is a branch for that purpose.

A similar code was found in OK Wave, but if you have reference books, we recommend that you specify the source.


2022-09-30 21:28

Originally, the authenticity determination in C language is determined by the int's 0 (false) or non-0 (true).
Therefore,

What is 1 in line 10-16 while(1)?

It will always be true and an infinite loop.

Is line 13 word_in=1; a declaration for an infinite loop?

Isn't word_in a conditional variable?

What does line 21 and line 28 mean by if(word_in)?

If word_in is anything other than zero, it will be ...

I wrote it down, but somehow I think it's an old program that I made for practice.
I don't think it's a very good code right now.


2022-09-30 21:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.