{"id":4563,"date":"2013-04-24T07:00:00","date_gmt":"2013-04-24T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2013\/04\/24\/dark-corners-of-cc-the-typedef-keyword-doesnt-need-to-be-the-first-word-on-the-line\/"},"modified":"2013-04-24T07:00:00","modified_gmt":"2013-04-24T07:00:00","slug":"dark-corners-of-cc-the-typedef-keyword-doesnt-need-to-be-the-first-word-on-the-line","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20130424-00\/?p=4563","title":{"rendered":"Dark corners of C\/C++: The typedef keyword doesn&#8217;t need to be the first word on the line"},"content":{"rendered":"<p><P>\nHere are some strange but legal declarations in C\/C++:\n<\/P>\n<PRE>\nint typedef a;\nshort unsigned typedef b;\n<\/PRE>\n<P>\nBy convention, the <CODE>typedef<\/CODE> keyword comes at the\nbeginning of the line, but this is not actually required by the\nlanguage.\nThe above declarations are equivalent to\n<\/P>\n<PRE>\ntypedef int a;\ntypedef short unsigned b;\n<\/PRE>\n<P>\nThe C language (but not C++) also permits you to say\n<CODE>typedef<\/CODE> without actually defining a type!\n<\/P>\n<PRE>\ntypedef enum { c }; \/\/ legal in C, not C++\n<\/PRE>\n<P>\nIn the above case, the <CODE>typedef<\/CODE> is ignored,\nand it&#8217;s the same as just declaring the <CODE>enum<\/CODE>\nthe plain boring way.\n<\/P>\n<PRE>\nenum { c };\n<\/PRE>\n<P>\nOther weird things you can do with <CODE>typedef<\/CODE> in C:\n<\/P>\n<PRE>\ntypedef;\ntypedef int;\ntypedef int short;\n<\/PRE>\n<P>\nNone of the above statements do anything,\nbut they are technically legal in pre-C89 versions of the C language.\nThey are just alternate manifestations of the quirk in the\ngrammar that permits you to say <CODE>typedef<\/CODE> without\nactually defining a type.\n(In C89,\nthis loophole was closed:\nClause 6.7 Constraint 2\nrequires that &#8220;A declaration shall declare at least\na declarator, a tag, or the members of an enumeration.&#8221;)\n<\/P>\n<P>\nThat last example of <CODE>typedef int short;<\/CODE> is particularly\nmisleading,\nsince at first glance it sounds like it&#8217;s redefining the <CODE>short<\/CODE>\ndata type.\nBut then you realize that <CODE>int short<\/CODE> and\n<CODE>short int<\/CODE> are equivalent,\nand this is just an empty declaration of the <CODE>short int<\/CODE> data type.\nIt doesn&#8217;t actually widen your shorts.\nIf you need to widen your shorts,\ngo see a tailor.&sup1;\n<P>\nNote that just because it&#8217;s legal doesn&#8217;t mean it&#8217;s recommended.\nYou should probably stick to using <CODE>typedef<\/CODE> the way\nmost people use it,\nunless you&#8217;re looking to enter the\n<A HREF=\"http:\/\/www.ioccc.org\/\">IOCCC<\/A>.\n<\/P>\n<P>\n&sup1; The primary purpose of this article was to tell that one stupid joke.\nAnd it&#8217;s not even my joke!\n<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here are some strange but legal declarations in C\/C++: int typedef a; short unsigned typedef b; By convention, the typedef keyword comes at the beginning of the line, but this is not actually required by the language. The above declarations are equivalent to typedef int a; typedef short unsigned b; The C language (but not [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-4563","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Here are some strange but legal declarations in C\/C++: int typedef a; short unsigned typedef b; By convention, the typedef keyword comes at the beginning of the line, but this is not actually required by the language. The above declarations are equivalent to typedef int a; typedef short unsigned b; The C language (but not [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/4563","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=4563"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/4563\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=4563"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=4563"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=4563"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}