Is `struct` keyword needed in declaring a variable

2019-07-21 20:35发布

问题:

I have come to know from book that for declaring a structure variable it is necessary a preceding struct keyword, but without that preceding struct in my Bloodshed\DevC++ compiler variable can be declared without any error like following,

struct stype
{
       int ival;
       float fval;
       double dval;
};

and in main,

stype s;
s.ival=10;s.dval=23.23;s.fval=233.23;
printf("%d %f %lf\n",s.ival,s.fval,s.dval);

This correctly prints what should be printed. Is there any modification behind using this struct keyword in variable declaration or what? why this code working??

回答1:

In C it is obligatory (or you can use a typedef). In C++ not.