Is `struct` keyword needed in declaring a variable of structure?

旧巷老猫 提交于 2019-12-11 02:28:01

问题


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.



来源:https://stackoverflow.com/questions/11364653/is-struct-keyword-needed-in-declaring-a-variable-of-structure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!