我是C的初学者,我正在尝试编写一个函数来返回一个字符串.我知道在C中我们没有字符串数据类型.而不是这些我尝试使用一组字符,但这不是我的解决方案.
char[] my_function(int x){
if(x>0)
return 'greaterThanZero';
else
return 'smallerOrEqualOfZero';
}
Run Code Online (Sandbox Code Playgroud)
请帮我.
返回类型需要,const char *并且字符串文字需要用双引号括起来:
const char * my_function(int x)
{
if (x > 0)
return "greaterThanZero";
else
return "lessThanOrEqualToZero";
}
int main(void)
{
printf("my_function(1) = %s\n", my_function(1));
printf("my_function(0) = %s\n", my_function(0));
printf("my_function(-1) = %s\n", my_function(-1));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
请注意,单引号用于char变量:
char c = 'X'; // single character - single quotes
char *s = "Hello world!"; // string - double quotes
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10346 次 |
| 最近记录: |