C语言数组问题

Hugh_OverAc 2014-11-21 09:21:21
#include <stdio.h>
#include <stdlib.h>

int main()
{
char word[100],ch,n;
int i=0;
word[i]=getchar();
while(word[i]!='\0') //这里!
{
i++;
word[i]=getchar();
}
n=i-1;
for(i=0;i<=n;i++)
printf("%c",word[i]);
return 0;
}


你们说while语句里为啥不能直接用数组判断??
...全文
74 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
li4c 2014-11-21
  • 打赏
  • 举报
回复
/*************************************************************************
    > File Name: mygetchar.c
    > Author: 傻李
    > Mail: hellojukay@gmail.com 
    > Created Time: 2014年11月21日 星期五 21时54分52秒
 ************************************************************************/
//#include<string.h>
#include<stdio.h>
int main()
{
	char a[100];
	int ch;
	int i=0;
	//memset(a,'\0',sizeof(a));
	while((ch = getchar()) != '\n' && i <100)
	{
		a[i++] = ch;
	}
	printf("%s\n",a);
	return 0;
}
Hugh_OverAc 2014-11-21
  • 打赏
  • 举报
回复
那用scanf试试?还是其他什么的,就是我输入回车,就停止输入了,我想得到这个,然后呢用下面这个好像是行的 #include <stdio.h> #include <stdlib.h> //#include <ctype.h> int main() { char ch,word[100]; int i=0,n; ch=getchar(); while(ch!='\n') { word[i++]=ch; ch=getchar(); } n=i-1; for(i=0;i<=n;i++) printf("%c",word[i]); return 0; } 怎么回事呢
li4c 2014-11-21
  • 打赏
  • 举报
回复
/* Copyright (C) 1993-2014 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <http://www.gnu.org/licenses/>.

   As a special exception, if you link the code in this file with
   files compiled with a GNU compiler to produce an executable,
   that does not cause the resulting executable to be covered by
   the GNU Lesser General Public License.  This exception does not
   however invalidate any other reasons why the executable file
   might be covered by the GNU Lesser General Public License.
   This exception applies to code released by its copyright holders
   in files containing the exception.  */

#include "libioP.h"
#include "stdio.h"

#undef getchar

int
getchar (void)
{
  int result;
  _IO_acquire_lock (_IO_stdin);
  result = _IO_getc_unlocked (_IO_stdin);
  _IO_release_lock (_IO_stdin);
  return result;
}

#if defined weak_alias && !defined _IO_MTSAFE_IO
#undef getchar_unlocked
weak_alias (getchar, getchar_unlocked)
#endif
li4c 2014-11-21
  • 打赏
  • 举报
回复
1.getchar()返回值是int 2.数组容会越界,除非你数组足够的

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧