简体   繁体   English

意外输出(C字符串和字符)

[英]Unexpected output (C strings and characters)

I'm just starting off with characters and arrays. 我刚刚开始使用字符和数组。 I wrote some code to generate a string of certain random and non-random characters. 我写了一些代码来生成一些随机和非随机字符串。 Well, actually it's supposed to generate bargain codes for Dominos pizza. 嗯,实际上它应该为Dominos披萨生成廉价代码。 Ahem... 咳咳...

Anyway, this is the code, and I'm perplexed by lack of any output whatsoever. 无论如何,这是代码,我对任何输出都没有感到困惑。 I compiled with gcc -Wall, and there's no warning or errors. 我用gcc -Wall编译,没有警告或错误。 So it's apparent that it's some fundamental structural aspect regarding strings. 所以很明显,这是关于字符串的一些基本结构方面。

I'd appreciate any insights regarding this. 我很感激有关这方面的任何见解。

Code: 码:

#include <stdio.h>
#include <stdlib.h> 
#include "conio.h"
#include <time.h>


int genrandom(int,int);
char randAlph(void);
char letterize(int);
char randDigit(void);
char digitize(int);
void weaver(void);
void prtall(char[],int);


int main (void) {

    srand(time(0));  
    weaver();
    return 0; 
}



void weaver(void) {
  //BG5C?---1
    char word[10];
    word[0]='B';
    word[1]='G';
    word[2]='5';
    word[3]='C';
    word[4]=randDigit();
    word[5]=randAlph();
    word[6]=randAlph();
    word[7]=randAlph();
    word[8]='\0';
    prtall(word,8);
}

void prtall(char worder[],int len){       
    int i;
    for (i=0;(i=len);i++) {
      if ( worder[i] != '\0' ){
    printf("%c",worder[i]);
      }
    }
    printf("\n");
}   

int genrandom(int mino,int maxo) {
    int val=mino+rand()/(RAND_MAX/(maxo-mino)+1);
    return val;  
}

char randAlph (void){
  int val;
  char text;
  val=genrandom(0,26);
  text=letterize(val);
  return text;

}

char randDigit () {
    int val;
    char text;
    val=genrandom(0,9);
    text=digitize(val);
    return text;                            
}

char letterize(int num) {
  char letter='A'+num;
  return letter;
}

char digitize(int num) {
  char digit='0'+num;
  return digit;
}
for (i=0;(i=len);i++)

应该

for (i=0;i<=len;i++)

Since you through the trouble to 0-terminate your array, just print it with: 由于您通过麻烦来终止数组,只需打印它:

puts(word);

or 要么

printf("%s\n", word);

to get the line-feed. 获得换行。

No need to loop yourself and print one character at a time. 无需循环自己并一次打印一个字符。

for (i=0;(i=len);i++)

Did you mean i <= len ? 你的意思是我<= len

i=len is an assignment , returns true if assignment succeeds. i=lenassignment ,如果赋值成功则返回true。

So what happens is 那么会发生什么

i gets assigned the value of len, (which is successful) hence returns true. 我被赋予len的值,(这是成功的)因此返回true。

Hence for loop condition gets satisfied. 因此,循环条件得到满足。

What you need is 你需要的是什么

           for (i=0;i<=len;i++)

Your stuck in an infinite loop, the problem is with the for statement: 你陷入无限循环,问题是与for语句:

for (i=0;(i=len);i++) {

You want i<=len , not i=len . 你想要i<=len ,而不是i=len Your for loop right now is doing: 你的for循环现在正在做:

i = 0, i = 8
if '\0' != '\0'
i++ (i = 9)

Then when it goes to "check" the condition it really just resets i to 8 然后,当它“检查”条件时,它实际上只是将i重置为8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM