【发布时间】:2015-04-07 12:15:19
【问题描述】:
以下有什么区别? (三)
char x[100] ;
char y[] = ""; // no space between the double quotations
char z[] = " "; // space between the double quotations
如果用户在数组 y 中输入了一个输入,例如“test”,它的大小会变为 5 吗?
char y[] ="";
gets(y); // user entered "test"
如果用户在数组 x 中输入大于 100 的输入,它的大小会改变吗?
char x[100] ;
gets(x); // user entered an input larger than 100
以及为什么这段代码有效:(如果用户输入“test”,它将打印“test”)
#include<stdio.h>
#include<string.h>
int main(){
char name[] = " " ; // space between the double quotations
gets(name);
for(int i=0 ; i< strlen(name) ; i++) {
printf("%c",name[i]);
}
return 0 ;
}
而这个没有? (这会打印奇怪的符号)(如果用户输入“test”,它将打印“t”和笑脸符号)
#include<stdio.h>
#include<string.h>
int main(){
char name[] = "" ; // no space between the double quotations
gets(name);
for(int i=0 ; i< strlen(name) ; i++) {
printf("%c",name[i]);
}
return 0 ;
}
这个让我抓狂,它没有循环,即使双引号之间没有空格
#include<stdio.h>
#include<string.h>
int main(){
char name[] = "" ; // no space between the double quotations
gets(name);
printf("%c",name[0]);
printf("%c",name[1]);
printf("%c",name[2]);
printf("%c",name[3]);
return 0 ;
}
即使双引号之间没有空格,这个也可以使用 ( puts ) :
#include<stdio.h>
#include<string.h>
int main(){
char name[] = "" ;
gets(name);
puts(name);
return 0 ;
}
【问题讨论】:
-
这是你的作业吗?
-
永远不要使用
gets(),特别是在你的情况下,这是一个非常糟糕的主意。 -
@SouravGhosh
'\0'呢? -
@SouravGhosh 是您的编译器显示的错误吗? ...我正在使用 DevCpp,它没有显示错误