struct happy 想在别的函数调用是一定要放在主函数外吗?

#include<stdio.h>

int main()
{
struct happy
{
int year;
int month;
int day;
};
int x(struct happy *q);
struct happy a;
x(&a);
return 0;
}
int x(struct happy *q)
{
printf(" ");
return 0;
}
18 14 D:\C合集\第九章\第二条.c [Warning] 'struct happy' declared inside parameter list will not be visible outside of this definition or declaration
18 5 D:\C合集\第九章\第二条.c [Error] conflicting types for 'x'
13 5 D:\C合集\第九章\第二条.c [Note] previous declaration of 'x' was here
D:\C合集\第九章\第二条.c In function 'x':
18 21 D:\C合集\第九章\第二条.c [Error] unused parameter 'q' [-Werror=unused-parameter]
D:C合集\第九章\cc1.exe some warnings being treated as errors

如果需要多个函数调用,则struct happy需要定义在函数外部,且定义在所有调用的函数的上方。
如果放在函数内部,则该结构体的作用域就只能是在其所在函数内部,其他外边的函数无法调用。
如果放在函数外部且放在需要调用的函数下部,由于c语言从上往下执行,在执行到调用的地方时没有发现该结构体则会报错。
所以,在定义时尽量放在其所在类的上方,这样方便所有用到地方调用。

你放在main函数里的话,只有main函数内的代码能认识这个结构,x函数是无法认识的,所以要放到main函数外面才行