有什么不对这个简单的code? [英] What is wrong with this simple code?

查看:145
本文介绍了有什么不对这个简单的code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这可能是一个简单的错误,但是我仍然无法弄清楚错误。我正在

指针地址,而不是当我打印出一个整数的值。

 #包括LT&;&stdio.h中GT;诠释主(){    炭S1 [100];    INT单词,行恰拉= 0;
    FILE * FP;    FP = FOPEN(fox.txt,R);    如果(FP == NULL){
        的printf(无法打开文件);
    }
    其他{
        而(的fscanf(FP,%S,S1)!= EOF){            也就是说++;        //的printf(%S,S1);
            }    }
        的printf(有字的%D字);
}

fox.txt

 快速棕色狐狸
跳过懒惰

输出:

 有字2665625


解决方案

将不会被初始化,所以你开始从一个未定义的值递增。

  INT字数,行数,甜心= 0;

设定甜心 0,但不初始化其他变量。如果你想初始化所有3,你需要

  INT字= 0,行= 0,甜心= 0;

I know this might be a simple error however i still cant figure out the error. I am getting

the pointer address instead of the value when i print out an integer number.

#include<stdio.h>

int main(){

    char s1[100];

    int words,lines,chara = 0;


    FILE * fp;

    fp  = fopen("fox.txt","r");

    if(fp==NULL){
        printf("Can't open file");
    }
    else{
        while (fscanf(fp,"%s",s1) != EOF){

            words++;

        //  printf("%s",s1);
            }

    }
        printf("There are %d of words",words);
}

fox.txt

The quick 

brown fox
jumps over

the lazy 


dog

output :

There are 2665625 of words

解决方案

words isn't initialised so you start incrementing from an undefined value.

int words,lines,chara = 0;

sets chara to 0 but doesn't initialise the other variables. If you want to initialise all 3, you'd need

int words = 0, lines = 0, chara = 0;

这篇关于有什么不对这个简单的code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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