From WikiChip
Difference between revisions of "c/assert.h/static assert"
< c‎ | assert.h

(Created page with "{{DISPLAYTITLE: static_assert macro - <assert.h> - C}} {{Assert.h - C}} The '''static_assert''' macro, which is part of <assert.h>, is a convenient object-li...")
 
m
 
(9 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{DISPLAYTITLE: static_assert macro - <assert.h> - C}}
+
{{c title|static_assert macro - <assert.h>}}{{Assert.h - C}}
{{Assert.h - C}}
+
The '''static_assert''' macro, which is part of {{C|assert.h|<assert.h>}}, is a convenient object-like macro name that expands to the '''{{C|_Static_assert}}''' keyword.
 
 
The '''static_assert''' macro, which is part of <[[assert.h - C|assert.h]]>, is a convenient object-like macro name that expands to the '''[[_Static_assert - C|_Static_assert]]''' keyword.
 
 
 
I.E.,
 
  
 +
== Synopsis ==
 
<source lang="c">#include <assert.h>
 
<source lang="c">#include <assert.h>
 
#define static_assert _Static_assert</source>
 
#define static_assert _Static_assert</source>
  
 
== Description ==
 
== Description ==
The static_assert object-like macro expands to the [[_Static_assert - C|_Static_assert]] keyboard, a keyboard added in [[C11]] to provide compile-time assertion.
+
The static_assert object-like macro expands to the {{C|_Static_assert}}, a keyword added in [[C11]] to provide compile-time assertion.
  
 
== Example ==
 
== Example ==
Line 27: Line 24:
 
<pre>example.c:3:1: error: static assertion failed "Code relies on int being exactly 4 bytes"</pre>
 
<pre>example.c:3:1: error: static assertion failed "Code relies on int being exactly 4 bytes"</pre>
  
{{DEFAULTSORT:static_assert - assert.h - C}}
+
{{DEFAULTSORT:static_assert}}
[[Category:C standard library]]
 
 
[[Category:assert.h - C]]
 
[[Category:assert.h - C]]
 +
[[Category:Assertion - C]]

Latest revision as of 07:32, 4 January 2015

The static_assert macro, which is part of <assert.h>, is a convenient object-like macro name that expands to the _Static_assert keyword.

Synopsis[edit]

#include <assert.h>
#define static_assert _Static_assert

Description[edit]

The static_assert object-like macro expands to the _Static_assert, a keyword added in C11 to provide compile-time assertion.

Example[edit]

#include <assert.h>

static_assert(sizeof(int) == 4, "Code relies on int being exactly 4 bytes");

int main(void)
{
    return 0;
}

On a machine where sizeof (int) is not 4, a constraint violation occurs. On such implementations, a diagnostic message is printed similar to the one below:

example.c:3:1: error: static assertion failed "Code relies on int being exactly 4 bytes"