C用于周期执行 [英] C For cycle execution

查看:127
本文介绍了C用于周期执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的程序(基于示例从C编程语言编2第1.5.2节的书)的字符计数。该程序本身是很简单的,但我没有得到期望的结果。

I have a simple program (based on example from The C programming language ed. 2 section 1.5.2 book) for characters counting. The program itself is very simple, however I am no getting the expecting results.

似乎 numberOfChars 会递增两次,我没有不为什么......?

It seems that numberOfChars is incremented twice and I do not not why...?

我使用的cygwin + gcc编译器,请参阅图片:

I am using cygwin + gcc compiler, please see the image:

int main () {   
    int numberOfChars;  

    for(numberOfChars = 0; getchar() != EOF; numberOfChars++) 
        ;   //null statement

    printf("Number of chars = %d\n", numberOfChars);

    return 0;
}

感谢任何帮助: - )

Thanks for any help :-)

推荐答案

创建一个文件,然后运行cygwin的OD程序,其次是C范例程序运行:

Create a file, then run the cygwin od program, followed by a run of the example C program :

od -x < testfile
a.out < testfile

然后,你可以看到什么和放大器;你有多少个字符的文件中有准确,避免意外。

Then you can see what & how many characters you had exactly in the file and avoid surprises.

辅助调试,如果我怀疑是出事了,我要补充的东西,如:

As a debugging aid, if I suspected something was wrong, I'd add something like :

int c; for(numberOfChars = 0; (c = getchar()) != EOF; numberOfChars++) 
    printf( "\0x%x", c);

要理解我的code是做处理。

To understand the processing my code was doing.

这篇关于C用于周期执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆