{"id":4616,"date":"2014-04-10T17:35:01","date_gmt":"2014-04-10T15:35:01","guid":{"rendered":"http:\/\/blog.zhaw.ch\/icclab\/?p=4616"},"modified":"2014-04-10T17:35:01","modified_gmt":"2014-04-10T15:35:01","slug":"empty-parameter-list-in-c-function-do-you-write-funcvoid-or-func","status":"publish","type":"post","link":"https:\/\/blog.zhaw.ch\/icclab\/empty-parameter-list-in-c-function-do-you-write-funcvoid-or-func\/","title":{"rendered":"Empty parameter list in C function, do you write func(void) or func()?"},"content":{"rendered":"<p>While reviewing code for the <a title=\"FI-PPP FI-WARE (KIARA)\" href=\"http:\/\/blog.zhaw.ch\/icclab\/fi-ppp-fi-ware-kiara\/\">KIARA<\/a> project I came across a change set which read like this:<\/p>\n<pre>\r\n- void super_duper_func () {\r\n+ void super_duper_func (void) {\r\n<\/pre>\n<p>I was puzzled, what&#8217;s the difference anyway except from making it explicitly clear that there are no parameter expected? Well, I was wrong. The <a title=\"ISO\/IEC 9899\/TC2 aka ISO C99\" href=\"http:\/\/www.open-std.org\/jtc1\/sc22\/wg14\/www\/docs\/n1124.pdf\">ISO 9899 standard (read: C99 standard)<\/a> states under paragraph &#8216;6.7.5.3 Function declarators (including prototypes)&#8217; that<\/p>\n<p>10 \u2014 The special case of an unnamed parameter of type void as the only item in the list<br \/>\nspecifies that the function has no parameters.<br \/>\n14 \u2014 An identifier list declares only the identifiers of the parameters of the function. An empty<br \/>\nlist in a function declarator that is part of a definition of that function specifies that the<br \/>\nfunction has no parameters. The empty list in a function declarator that is not part of a<br \/>\ndefinition of that function specifies that no information about the number or types of the<br \/>\nparameters is supplied.<\/p>\n<p>Therefore we can conclude that even though your code may compile and work correctly, your code is not standard compliant and you may even leap a compile time error detection. Have a look a this snippet which compiled flawless with clang 3.4:<\/p>\n<pre>\r\n#include \r\n\r\nvoid func();\r\n\r\nint main() {\r\n    func(\"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\");\r\n    return 0;\r\n}\r\n\r\nvoid func() {\r\n    printf(\"in func()\\n\");\r\n}\r\n<\/pre>\n<p>Though when you turn on all warnings in clang you will get a <b>warning<\/b> but this is easily overlooked and not very obvious:<\/p>\n<pre>\r\n$ clang -std=c99 -Weverything -o empty_param_list empty_param_list.c\r\nempty_param_list.c:10:6: warning: no previous prototype for function 'func' [-Wmissing-prototypes]\r\nvoid func() {\r\n     ^\r\nempty_param_list.c:3:6: note: this declaration is not a prototype; add 'void' to make it a prototype for a zero-parameter function\r\nvoid func();\r\n     ^\r\n          void\r\n1 warning generated.\r\n<\/pre>\n<p>If you go through the code you will find a function prototype and you may think that if there was no previous prototype and the function is defined later than &#8216;main&#8217; the compiler will fail anyway \u2026 In this case if you forgot the function prototype the compiler would throw an error (conflicting types for &#8216;func&#8217;) even if you passed no arguments.<\/p>\n<p>To sum it up:<\/p>\n<ul>\n<li>Create function prototypes\/declarations (they go before the first function definition in your source)<\/li>\n<li>If you don&#8217;t need any parameters explicitly write void in the parameter list (helps you with finding mistakes)<\/li>\n<li>Turn on all warnings with either &#8216;-Wall&#8217; (gcc) or &#8216;-Weverything&#8217; (clang) and don&#8217;t ignore those warnings!<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>While reviewing code for the KIARA project I came across a change set which read like this: &#8211; void super_duper_func () { + void super_duper_func (void) { I was puzzled, what&#8217;s the difference anyway except from making it explicitly clear that there are no parameter expected? Well, I was wrong. The ISO 9899 standard (read: [&hellip;]<\/p>\n","protected":false},"author":61,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[5,15],"tags":[],"features":[],"class_list":["post-4616","post","type-post","status-publish","format-standard","hentry","category-articles","category-howtos"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.2 (Yoast SEO v27.2) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Empty parameter list in C function, do you write func(void) or func()? - Service Engineering (ICCLab &amp; SPLab)<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blog.zhaw.ch\/icclab\/empty-parameter-list-in-c-function-do-you-write-funcvoid-or-func\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Empty parameter list in C function, do you write func(void) or func()?\" \/>\n<meta property=\"og:description\" content=\"While reviewing code for the KIARA project I came across a change set which read like this: - void super_duper_func () { + void super_duper_func (void) { I was puzzled, what&#8217;s the difference anyway except from making it explicitly clear that there are no parameter expected? Well, I was wrong. The ISO 9899 standard (read: [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blog.zhaw.ch\/icclab\/empty-parameter-list-in-c-function-do-you-write-funcvoid-or-func\/\" \/>\n<meta property=\"og:site_name\" content=\"Service Engineering (ICCLab &amp; SPLab)\" \/>\n<meta property=\"article:published_time\" content=\"2014-04-10T15:35:01+00:00\" \/>\n<meta name=\"author\" content=\"habl\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"habl\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/blog.zhaw.ch\/icclab\/empty-parameter-list-in-c-function-do-you-write-funcvoid-or-func\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/blog.zhaw.ch\/icclab\/empty-parameter-list-in-c-function-do-you-write-funcvoid-or-func\/\"},\"author\":{\"name\":\"habl\",\"@id\":\"https:\/\/blog.zhaw.ch\/icclab\/#\/schema\/person\/db8c433b7da8ab174c359b95d38c34ac\"},\"headline\":\"Empty parameter list in C function, do you write func(void) or func()?\",\"datePublished\":\"2014-04-10T15:35:01+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/blog.zhaw.ch\/icclab\/empty-parameter-list-in-c-function-do-you-write-funcvoid-or-func\/\"},\"wordCount\":338,\"commentCount\":1,\"articleSection\":[\"Articles\",\"HowTos\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/blog.zhaw.ch\/icclab\/empty-parameter-list-in-c-function-do-you-write-funcvoid-or-func\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/blog.zhaw.ch\/icclab\/empty-parameter-list-in-c-function-do-you-write-funcvoid-or-func\/\",\"url\":\"https:\/\/blog.zhaw.ch\/icclab\/empty-parameter-list-in-c-function-do-you-write-funcvoid-or-func\/\",\"name\":\"Empty parameter list in C function, do you write func(void) or func()? - Service Engineering (ICCLab &amp; SPLab)\",\"isPartOf\":{\"@id\":\"https:\/\/blog.zhaw.ch\/icclab\/#website\"},\"datePublished\":\"2014-04-10T15:35:01+00:00\",\"author\":{\"@id\":\"https:\/\/blog.zhaw.ch\/icclab\/#\/schema\/person\/db8c433b7da8ab174c359b95d38c34ac\"},\"breadcrumb\":{\"@id\":\"https:\/\/blog.zhaw.ch\/icclab\/empty-parameter-list-in-c-function-do-you-write-funcvoid-or-func\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/blog.zhaw.ch\/icclab\/empty-parameter-list-in-c-function-do-you-write-funcvoid-or-func\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/blog.zhaw.ch\/icclab\/empty-parameter-list-in-c-function-do-you-write-funcvoid-or-func\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Startseite\",\"item\":\"https:\/\/blog.zhaw.ch\/icclab\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Empty parameter list in C function, do you write func(void) or func()?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/blog.zhaw.ch\/icclab\/#website\",\"url\":\"https:\/\/blog.zhaw.ch\/icclab\/\",\"name\":\"Service Engineering (ICCLab &amp; SPLab)\",\"description\":\"A Blog of the ZHAW Zurich University of Applied Sciences\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/blog.zhaw.ch\/icclab\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/blog.zhaw.ch\/icclab\/#\/schema\/person\/db8c433b7da8ab174c359b95d38c34ac\",\"name\":\"habl\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/secure.gravatar.com\/avatar\/c97d6aa6e5a0ea4cea97629d05d3d278bb904dabc955d75727ec9c45b1251ea7?s=96&d=mm&r=g\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c97d6aa6e5a0ea4cea97629d05d3d278bb904dabc955d75727ec9c45b1251ea7?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c97d6aa6e5a0ea4cea97629d05d3d278bb904dabc955d75727ec9c45b1251ea7?s=96&d=mm&r=g\",\"caption\":\"habl\"},\"url\":\"https:\/\/blog.zhaw.ch\/icclab\/author\/habl\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Empty parameter list in C function, do you write func(void) or func()? - Service Engineering (ICCLab &amp; SPLab)","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blog.zhaw.ch\/icclab\/empty-parameter-list-in-c-function-do-you-write-funcvoid-or-func\/","og_locale":"en_US","og_type":"article","og_title":"Empty parameter list in C function, do you write func(void) or func()?","og_description":"While reviewing code for the KIARA project I came across a change set which read like this: - void super_duper_func () { + void super_duper_func (void) { I was puzzled, what&#8217;s the difference anyway except from making it explicitly clear that there are no parameter expected? Well, I was wrong. The ISO 9899 standard (read: [&hellip;]","og_url":"https:\/\/blog.zhaw.ch\/icclab\/empty-parameter-list-in-c-function-do-you-write-funcvoid-or-func\/","og_site_name":"Service Engineering (ICCLab &amp; SPLab)","article_published_time":"2014-04-10T15:35:01+00:00","author":"habl","twitter_card":"summary_large_image","twitter_misc":{"Written by":"habl","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blog.zhaw.ch\/icclab\/empty-parameter-list-in-c-function-do-you-write-funcvoid-or-func\/#article","isPartOf":{"@id":"https:\/\/blog.zhaw.ch\/icclab\/empty-parameter-list-in-c-function-do-you-write-funcvoid-or-func\/"},"author":{"name":"habl","@id":"https:\/\/blog.zhaw.ch\/icclab\/#\/schema\/person\/db8c433b7da8ab174c359b95d38c34ac"},"headline":"Empty parameter list in C function, do you write func(void) or func()?","datePublished":"2014-04-10T15:35:01+00:00","mainEntityOfPage":{"@id":"https:\/\/blog.zhaw.ch\/icclab\/empty-parameter-list-in-c-function-do-you-write-funcvoid-or-func\/"},"wordCount":338,"commentCount":1,"articleSection":["Articles","HowTos"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/blog.zhaw.ch\/icclab\/empty-parameter-list-in-c-function-do-you-write-funcvoid-or-func\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/blog.zhaw.ch\/icclab\/empty-parameter-list-in-c-function-do-you-write-funcvoid-or-func\/","url":"https:\/\/blog.zhaw.ch\/icclab\/empty-parameter-list-in-c-function-do-you-write-funcvoid-or-func\/","name":"Empty parameter list in C function, do you write func(void) or func()? - Service Engineering (ICCLab &amp; SPLab)","isPartOf":{"@id":"https:\/\/blog.zhaw.ch\/icclab\/#website"},"datePublished":"2014-04-10T15:35:01+00:00","author":{"@id":"https:\/\/blog.zhaw.ch\/icclab\/#\/schema\/person\/db8c433b7da8ab174c359b95d38c34ac"},"breadcrumb":{"@id":"https:\/\/blog.zhaw.ch\/icclab\/empty-parameter-list-in-c-function-do-you-write-funcvoid-or-func\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blog.zhaw.ch\/icclab\/empty-parameter-list-in-c-function-do-you-write-funcvoid-or-func\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/blog.zhaw.ch\/icclab\/empty-parameter-list-in-c-function-do-you-write-funcvoid-or-func\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Startseite","item":"https:\/\/blog.zhaw.ch\/icclab\/"},{"@type":"ListItem","position":2,"name":"Empty parameter list in C function, do you write func(void) or func()?"}]},{"@type":"WebSite","@id":"https:\/\/blog.zhaw.ch\/icclab\/#website","url":"https:\/\/blog.zhaw.ch\/icclab\/","name":"Service Engineering (ICCLab &amp; SPLab)","description":"A Blog of the ZHAW Zurich University of Applied Sciences","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blog.zhaw.ch\/icclab\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/blog.zhaw.ch\/icclab\/#\/schema\/person\/db8c433b7da8ab174c359b95d38c34ac","name":"habl","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/c97d6aa6e5a0ea4cea97629d05d3d278bb904dabc955d75727ec9c45b1251ea7?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c97d6aa6e5a0ea4cea97629d05d3d278bb904dabc955d75727ec9c45b1251ea7?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c97d6aa6e5a0ea4cea97629d05d3d278bb904dabc955d75727ec9c45b1251ea7?s=96&d=mm&r=g","caption":"habl"},"url":"https:\/\/blog.zhaw.ch\/icclab\/author\/habl\/"}]}},"_links":{"self":[{"href":"https:\/\/blog.zhaw.ch\/icclab\/wp-json\/wp\/v2\/posts\/4616","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.zhaw.ch\/icclab\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.zhaw.ch\/icclab\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.zhaw.ch\/icclab\/wp-json\/wp\/v2\/users\/61"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.zhaw.ch\/icclab\/wp-json\/wp\/v2\/comments?post=4616"}],"version-history":[{"count":1,"href":"https:\/\/blog.zhaw.ch\/icclab\/wp-json\/wp\/v2\/posts\/4616\/revisions"}],"predecessor-version":[{"id":4617,"href":"https:\/\/blog.zhaw.ch\/icclab\/wp-json\/wp\/v2\/posts\/4616\/revisions\/4617"}],"wp:attachment":[{"href":"https:\/\/blog.zhaw.ch\/icclab\/wp-json\/wp\/v2\/media?parent=4616"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.zhaw.ch\/icclab\/wp-json\/wp\/v2\/categories?post=4616"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.zhaw.ch\/icclab\/wp-json\/wp\/v2\/tags?post=4616"},{"taxonomy":"features","embeddable":true,"href":"https:\/\/blog.zhaw.ch\/icclab\/wp-json\/wp\/v2\/features?post=4616"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}