2

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??

2
  • c-faq.com/struct/impltypedef.html Commented Jul 6, 2012 at 14:59
  • 4
    Your compiler is compiling the file as a C++ file instead of a C file. Fix this setting and the code above will not compile. Commented Jul 6, 2012 at 15:01

1 Answer 1

8

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

Sign up to request clarification or add additional context in comments.

2 Comments

So what u are saying I am using C++ compiler thats why it is working here??
Yes, this is exactly what I'm saying.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.