修改后的程序无法正常运行

修改后的程序无法正常运行

本文介绍了C编程-K& R示例1.5.2-修改后的程序无法正常运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题很简单:为什么第10行和第11行的代码无法正常运行?"我的代码的预期目的与原始K& R代码完全相同,但是无论何时(getchar()=='\ n')都请您启发我?

My question is simply "Why does my code on line 10 and 11 not function properly?" The intended purpose of my code is to do exactly as the original K&R code intended, but to NOT count nc whenever (getchar() == '\n') will you please enlighten me?

稍作修改的K& R代码:

slightly modified K&R code:

/** K&R - 1.5.2 Character Counting **/
#include <stdio.h>

/* count characters in input; 1st version */
main(){
  long nc;

  nc = 0;
  while (getchar() != EOF){
    if (getchar() != '\n'){
      ++nc;
    }
  }
  printf("%ld\n", nc);
}

我使用64位Windows 7,CodeBlocks10.05,GNU GCC编译器.

我目前的进步和理解:

在示例运行中,我输入单词two并按Enter键,它等于4个输入,然后按ctrl + Z输入^Z或EOF字符.然后程序打印1.我期望它能打印3.我想唯一的逻辑解释是,它所做的与我的意图完全相反( only 只能计算换行符?).事实证明,如果我输入单词two并按Enter键,可以说4次,它将打印4.输入的每个换行符似乎都在计数nc,但是如果我先按Enter键(在这种情况下为4次),然后按EOF,则它将始终打印0.经过进一步的实验,对于这个程序,看不见的4也许是一个神奇的数字.如果我启动它,然后完全按Enter键(一个可被4整除的数字),然后进行EOF,它将打印0.但是,如果我按回车键几次,EOF则什么也不做,我必须在^Z中输入两行,一个接一个地输入,以正确结束while循环,并显示1.这真让我难以置信!

on a sample run, i type in the word two and hit enter, which equals 4 inputs, after which i press ctrl+Z to enter in a ^Z or EOF character. The program then prints 1. I was expecting it to print 3. I suppose the only logical explanation is that it is doing exactly the opposite of what I intended (it only counts newline characters?). As it turns out, if I type in the word two and press enter, lets say 4 times, it prints 4. It seems to be counting nc for every newline character entered, but yet if I press enter alone (in this case 4 times) and then EOF, it always prints 0. Upon further experimentation, by some hand unseen 4 is perhaps a magical number for this program. If I start it up and hit enter key exactly (a number divisible by 4) times and then EOF it prints 0. However if i hit enter some other number of times the EOF does nothing, and I must enter in ^Z two rows, one after the other, to end the while loop correctly, and it prints 1. This is boggling my mind!

推荐答案

麻烦的是,您需要将getchar()中的值保存在int中,因为每次递增两个字符时都读取两个字符.数数.其中之一是在EOF测试中;第二个是在换行测试中.

The trouble is that you need to save the value from getchar() – in an int – because you are reading two characters for each time you increment the count. One of those is in the EOF test; the second is in the newline test.

int c;

while ((c = getchar()) != EOF)
{
    if (c != '\n')
        ++nc;
}


之所以需要将getchar()的结果存储在int中而不是在char中的原因是,它可以返回每个可能的char值以及一个不同的值EOF.如果不使用int(直接存储到char中),则会发生以下两种情况之一:


The reason you need to store the result of getchar() in an int and not a char is that it can return every possible char value and also a distinct value, EOF. If you don't use int (you store direct into a char), one of two things will happen:

  1. 如果char是带符号的类型,则将使用合法字符(通常是y-umlaut,ÿ,带有DIAERESIS的拉丁小写字母Y,U + 00FF,至少在源自拉丁语1或ISO 8859-1的代码集中)解释为等同于EOF,您的程序将提前终止.
  2. 如果char是无符号类型,则没有字符将等效于EOF,因此程序将永远不会停止循环.
  1. If char is a signed type, a legitimate character (often y-umlaut, ÿ, LATIN SMALL LETTER Y WITH DIAERESIS, U+00FF — at least in codesets derived from Latin 1 or ISO 8859-1) will be interpreted as equivalent to EOF, and your program will terminate prematurely.
  2. If char is an unsigned type, no character will ever be equivalent to EOF, so the program will never stop the loop.

这两种情况都不可取.将getchar()的返回值存储在int中可以防止这两个问题.这是唯一"(或至少是最简单)的正确方法.

Neither of these circumstances is desirable. Storing the return value of getchar() in an int prevents both problems; it is the 'only' (or, at least, the simplest) correct way to do it.

这篇关于C编程-K&amp; R示例1.5.2-修改后的程序无法正常运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 12:40