今天研究goto语句,突然看到有网友说用goto语句来计算键盘输入字符长度,觉得挺好用,在此贴出来:
#include<stdio.h>
int count;
void getCharLength()
{
printf("input a string:\n");
calLength:
if(getchar() != '\n')
{
count++;
goto calLength;
}
printf("output:%d\n",count);
}
int main()
{
getCharLength();
return 0;
}
如果敲回车即就可以跳出循环,然后计算出输入的这段字符串的长度。