From WikiChip
Editing c/static assertion

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.

This page supports semantic in-text annotations (e.g. "[[Is specified as::World Heritage Site]]") to build structured and queryable content provided by Semantic MediaWiki. For a comprehensive description on how to use annotations or the #ask parser function, please have a look at the getting started, in-text annotation, or inline queries help pages.

Latest revision Your text
Line 1: Line 1:
{{c title|Static Assertion}}
+
{{C Guide}}
The '''_Static_assert''' is a keyword added in the [[C11]] standard version of the [[C Programming Language]] that provides a compile-time assertions mechanism to the language. The keyword was added as a better way of performing compile-time logic checking over previous existing solutions, primarily via the {{C|error directive|#error directive}} and negative array sizes.
+
The '''_Static_assert''' is a keyword added in the [[C11]] standard version of the [[C Programming Language]] that provides a compile-time assertions mechanism to the language. The keyword was added as a better way of performing compile-time logic checking over previous existing solutions, primarily via the [[error directive - C|#error directive]] and negative array sizes.
  
 
== Overview ==
 
== Overview ==
Prior to the [[C11]] standard the primary mean of producing a compile-time error message was via the '''{{C|error directive|#error}}''' directive which has been part of the language since [[C89]] which causes the implementation to produce a diagnostic message that includes the message that followed it.
+
Prior to the [[C11]] standard the primary mean of producing a compile-time error message was via the '''[[error directive - C|#error]]''' directive which has been part of the language since [[C89]] which causes the implementation to produce a diagnostic message that includes the message that followed it.
  
A common way of determining size of an {{C|int}} was to use some code along the following lines:
+
A common way of determining size of an [[int - C|int]] was to use some code along the following lines:
  
 
<source lang="c">#if INT_MAX <= 65535
 
<source lang="c">#if INT_MAX <= 65535
Line 13: Line 13:
 
#endif</source>
 
#endif</source>
  
The code above works for simple tasks. However, often a more complex task is required such as using the {{C|sizeof operator}}. Unfortunately, tokens such as 'sizeof' are not converted to source tokens until after the {{C|preprocessing translation stage}} at which point you can no longer use preprocessing directives.
+
The code above works for simple tasks. However, often a more complex task is required such as using the [[sizeof operator - C|sizeof operator]]. Unfortunately, tokens such as 'sizeof' are not converted to source tokens until after the [[preprocessing translation stage]] at which point you can no longer use preprocessing directives.
  
 
For example:
 
For example:
Line 26: Line 26:
 
<source lang="c">_Static_assert(constant-expression, string-literal);</source>
 
<source lang="c">_Static_assert(constant-expression, string-literal);</source>
  
_Static_assert takes an integer constant expression. If the constant expression compares unequal to 0, the declaration has no effect. Otherwise it is assumed to produce a constraint violation and the implementation is required to produce a diagnostic message. The diagnostic message must include all the text of from the string-literal (with the exception of any characters that are not in the {{C|basic source character set}}).
+
_Static_assert takes an integer constant expression. If the constant expression compares unequal to 0, the declaration has no effect. Otherwise it is assumed to produce a constraint violation and the implementation is required to produce a diagnostic message. The diagnostic message must include all the text of from the string-literal (with the exception of any characters that are not in the [[basic source character set - C|basic source character set]]).
  
 
== Examples ==
 
== Examples ==
Line 45: Line 45:
  
 
== Convenience macro ==
 
== Convenience macro ==
[[C11]] defines a convenience macro {{C|assert.h/static_assert|static_assert}} in {{C|assert.h|<assert.h>}} which expands to _Static_assert.
+
[[C11]] defines a convenience macro [[assert.h/static_assert - C|static_assert]] in [[assert.h - C|<assert.h>]] which expands to _Static_assert.
  
 
== Alternative solutions ==
 
== Alternative solutions ==

Please note that all contributions to WikiChip may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see WikiChip:Copyrights for details). Do not submit copyrighted work without permission!

Cancel | Editing help (opens in new window)