C编程初学者 - 请解释这个错误

Joe*_*hew 6 c arrays

我刚刚从C开始,正在尝试Ritchie的书中的一些例子.我写了一个小程序来理解字符数组,但偶然发现了一些错误,并希望对我所理解的错误有所了解:

#include <stdio.h>
#define ARRAYSIZE 50
#include <string.h>

main () {
  int c,i;
  char letter[ARRAYSIZE];
  i=0;
  while ((c=getchar()) != EOF )
  {    
    letter[i]=c;
    i++;
  }
  letter[i]='\0';
  printf("You entered %d characters\n",i);
  printf("The word is ");

  printf("%s\n",letter);
  printf("The length of string is %d",strlen(letter));
  printf("Splitting the string into chars..\n");
  int j=0;
  for (j=0;j++;(j<=strlen(letter)))
    printf("The letter is %d\n",letter[j]);
}
Run Code Online (Sandbox Code Playgroud)

输出是:

$ ./a.out 
hello how are youYou entered 17 characters
The word is hello how are you
The length of string is 17Splitting the string into chars..
Run Code Online (Sandbox Code Playgroud)

怎么了?为什么for循环不提供任何输出?

ala*_*and 11

语法应该是;

for (j=0; j<strlen(letter); j++)
Run Code Online (Sandbox Code Playgroud)

既然strlen是costy操作,并且你不修改循环内的字符串,最好这样写:

const int len = strlen(letter);
for (j=0; j<=len; j++)
Run Code Online (Sandbox Code Playgroud)

此外,强烈建议在使用C字符串和用户输入时始终检查缓冲区溢出:

while ((c=getchar()) != EOF && i < ARRAYSIZE - 1)
Run Code Online (Sandbox Code Playgroud)


Bla*_*ear 7

错误在for中,只需交换结束条件和增量,如下所示:

for (j = 0; j <= strlen(letter); j++)
Run Code Online (Sandbox Code Playgroud)

问题: 最后一个角色是什么?