【发布时间】:2014-03-17 09:57:02
【问题描述】:
我正在玩 C,并决定构建自己的 strlen() 函数并且我做到了。但是通过这样做,我很震惊地发现 {} 可以做什么。这是我的代码:
#include <stdio.h>
#include <assert.h>
int mystrlen(char *s)
{
int i = 0;
assert(s[0] !='\0');
for(i = 0; s[i] !='\0'; i++)
// Placing {} here causes the error to disappear //
return i;
}
int main(void)
{
char hello[] = "hello";
int len = mystrlen(hello);
printf("Length = %d\n", len);
return 0;
}
运行此代码会产生此错误:
mystrlen.c:12:1: warning: control reaches end of non-void function [-Wreturn-type]
但是在放置 {} 在while 循环之后不再出现错误。有人可以向我解释这种行为吗?
【问题讨论】: