Is using a while block to do nothing a bad thing?

后端 未结 12 799
予麋鹿
予麋鹿 2021-02-01 03:29

I\'m currently working through the excercises in \'The C Programming Language\'. Here\'s one of my solutions:

int c;

while ((c=getchar()) != EOF) {

if (c == \         


        
12条回答
  •  情歌与酒
    2021-02-01 04:19

    I think it is perfectly acceptable.

    I would either write it:

    //skip all spaces
    while ((c = getchar()) == ' ') {} 
    

    to make it obvious that this one line of code does one thing.

    Or I would write it like this:

    while ((c = getchar()) == ' ') {
        //no processing required for spaces
    }
    

    so that it matches the rest of your code's format.

    Personally, I am not a fan of the

    while ((c = getchar()) == ' ');
    

    format. I think it is to easy to overlook the semi-colon.

提交回复
热议问题