Case 1: structure declared outside main()
working fine
#include
#include
struct prod
{
int price,
It have to do about scoping, when you define the structure inside the main
function then it's only defined in the scope of the main
function, so the billamt
function can't know about it.
where to declare structures, inside
main()
or outsidemain()
?
First thing, I think you meant "define", not "declare".
Second, there is no rule as such, You can define wherever you want. It is all about the scope of the definition.
If you define the structure inside main()
, the scope is limited to main()
only. Any other function cannot see that definition and hence, cannot make use of that structure definition.
If you define the structure in a global scope, (i.e., outside main()
or any other function, for that matter), that definition is available globally and all the functions can see and make use of the structure definition.
Structure are a like array only the main difference in Array can hold only same type of values but a structure can have different types of values so if you need to implement structure globally(by globally i mean it can be used in any other function too) define it outside the main and if you want to use your structure only in the main function define it inside it. Happy Coding :-)