1afe61c15SRodney W. Grimes.\" Copyright (c) 1991, 1993 2afe61c15SRodney W. Grimes.\" The Regents of the University of California. All rights reserved. 3afe61c15SRodney W. Grimes.\" 4afe61c15SRodney W. Grimes.\" Redistribution and use in source and binary forms, with or without 5afe61c15SRodney W. Grimes.\" modification, are permitted provided that the following conditions 6afe61c15SRodney W. Grimes.\" are met: 7afe61c15SRodney W. Grimes.\" 1. Redistributions of source code must retain the above copyright 8afe61c15SRodney W. Grimes.\" notice, this list of conditions and the following disclaimer. 9afe61c15SRodney W. Grimes.\" 2. Redistributions in binary form must reproduce the above copyright 10afe61c15SRodney W. Grimes.\" notice, this list of conditions and the following disclaimer in the 11afe61c15SRodney W. Grimes.\" documentation and/or other materials provided with the distribution. 12dda5b397SEitan Adler.\" 3. Neither the name of the University nor the names of its contributors 13afe61c15SRodney W. Grimes.\" may be used to endorse or promote products derived from this software 14afe61c15SRodney W. Grimes.\" without specific prior written permission. 15afe61c15SRodney W. Grimes.\" 16afe61c15SRodney W. Grimes.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 17afe61c15SRodney W. Grimes.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18afe61c15SRodney W. Grimes.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19afe61c15SRodney W. Grimes.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 20afe61c15SRodney W. Grimes.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21afe61c15SRodney W. Grimes.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22afe61c15SRodney W. Grimes.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23afe61c15SRodney W. Grimes.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24afe61c15SRodney W. Grimes.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25afe61c15SRodney W. Grimes.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26afe61c15SRodney W. Grimes.\" SUCH DAMAGE. 27afe61c15SRodney W. Grimes.\" 28*33f8d79dSFaraz Vahedi.Dd April 20, 2021 29afe61c15SRodney W. Grimes.Dt ASSERT 3 30afe61c15SRodney W. Grimes.Os 31afe61c15SRodney W. Grimes.Sh NAME 32*33f8d79dSFaraz Vahedi.Nm assert , 33*33f8d79dSFaraz Vahedi.Nm static_assert 34afe61c15SRodney W. Grimes.Nd expression verification macro 35afe61c15SRodney W. Grimes.Sh SYNOPSIS 3632eef9aeSRuslan Ermilov.In assert.h 37afe61c15SRodney W. Grimes.Fn assert expression 38*33f8d79dSFaraz Vahedi.Fn static_assert expression 39*33f8d79dSFaraz Vahedi.Fn static_assert expression message 40afe61c15SRodney W. Grimes.Sh DESCRIPTION 41afe61c15SRodney W. GrimesThe 42afe61c15SRodney W. Grimes.Fn assert 43afe61c15SRodney W. Grimesmacro tests the given 44afe61c15SRodney W. Grimes.Ar expression 45afe61c15SRodney W. Grimesand if it is false, 46afe61c15SRodney W. Grimesthe calling process is terminated. 472a9836a5SWarner LoshA diagnostic message is written to 48ae828962SRuslan Ermilov.Dv stderr 49afe61c15SRodney W. Grimesand the function 50671e66d2SAlexander Langer.Xr abort 3 512038017bSKris Kennawayis called, effectively terminating the program. 52afe61c15SRodney W. Grimes.Pp 53afe61c15SRodney W. GrimesIf 54afe61c15SRodney W. Grimes.Ar expression 55afe61c15SRodney W. Grimesis true, 56afe61c15SRodney W. Grimesthe 57afe61c15SRodney W. Grimes.Fn assert 58afe61c15SRodney W. Grimesmacro does nothing. 59afe61c15SRodney W. Grimes.Pp 60afe61c15SRodney W. GrimesThe 61afe61c15SRodney W. Grimes.Fn assert 62afe61c15SRodney W. Grimesmacro 638a78a4d2SGarrett Wollmanmay be removed at compile time by defining 648a78a4d2SGarrett Wollman.Dv NDEBUG 658a78a4d2SGarrett Wollmanas a macro 668a78a4d2SGarrett Wollman(e.g., by using the 678a78a4d2SGarrett Wollman.Xr cc 1 688a78a4d2SGarrett Wollmanoption 698a78a4d2SGarrett Wollman.Fl D Ns Dv NDEBUG ) . 70a1895a92SWarner LoshUnlike most other include files, 71a1895a92SWarner Losh.In assert.h 72a1895a92SWarner Loshmay be included multiple times. 73a1895a92SWarner LoshEach time whether or not 74a1895a92SWarner Losh.Dv NDEBUG 75a1895a92SWarner Loshis defined determines the behavior of assert from that point forward 76133d84f4SWarner Loshuntil the end of the unit or another include of 77a1895a92SWarner Losh.In assert.h . 782a9836a5SWarner Losh.Pp 792a9836a5SWarner LoshThe 802a9836a5SWarner Losh.Fn assert 812a9836a5SWarner Loshmacro should only be used for ensuring the developer's expectations 822a9836a5SWarner Loshhold true. 832a9836a5SWarner LoshIt is not appropriate for regular run-time error detection. 84*33f8d79dSFaraz Vahedi.Pp 85*33f8d79dSFaraz VahediThe 86*33f8d79dSFaraz Vahedi.Fn static_assert 87*33f8d79dSFaraz Vahedimacro expands to 88*33f8d79dSFaraz Vahedi.Fn _Static_assert , 89*33f8d79dSFaraz Vahediand, contrarily to 90*33f8d79dSFaraz Vahedi.Fn assert , 91*33f8d79dSFaraz Vahedimakes assertions at compile-time. 92*33f8d79dSFaraz VahediOnce the constraint is violated, the compiler produces a diagnostic 93*33f8d79dSFaraz Vahedimessage including the string literal message, if provided. 94*33f8d79dSFaraz VahediThe initial form of the 95*33f8d79dSFaraz Vahedi.Fn _Static_assert 96*33f8d79dSFaraz Vahedicontaining a string literal message was introduced in C11 standard, and 97*33f8d79dSFaraz Vahedithe other form with no string literal is to be implemented by C2x and 98*33f8d79dSFaraz Vahedisome compilers may lack its adoption at present. 99cb5f4605SMike Barcroft.Sh EXAMPLES 100cb5f4605SMike BarcroftThe assertion: 1013a021a47SRuslan Ermilov.Dl "assert(1 == 0);" 102cb5f4605SMike Barcroftgenerates a diagnostic message similar to the following: 1032a9836a5SWarner Losh.Dl "Assertion failed: (1 == 0), function main, file main.c, line 100." 1043a021a47SRuslan Ermilov.Pp 1052a9836a5SWarner LoshThe following assert tries to assert there was no partial read: 1062a9836a5SWarner Losh.Dl "assert(read(fd, buf, nbytes) == nbytes);" 1072a9836a5SWarner LoshHowever, there are two problems. 1082a9836a5SWarner LoshFirst, it checks for normal conditions, rather than conditions that 1092a9836a5SWarner Loshindicate a bug. 1102a9836a5SWarner LoshSecond, the code will disappear if 1112a9836a5SWarner Losh.Dv NDEBUG 1122a9836a5SWarner Loshis defined, changing the semantics of the program. 113*33f8d79dSFaraz Vahedi.Pp 114*33f8d79dSFaraz VahediThe following asserts that the size of the S structure is 16. 115*33f8d79dSFaraz VahediOtherwise, it produces a diagnostic message which points at the 116*33f8d79dSFaraz Vahediconstraint and includes the provided string literal: 117*33f8d79dSFaraz Vahedi.Dl "static_assert(sizeof(struct S) == 16, ""size mismatch"");" 118*33f8d79dSFaraz VahediIf none is provided, it only points at the constraint. 119e69bcfc3SAlexander Langer.Sh SEE ALSO 1204c72d794SEric van Gyzen.Xr abort2 2 , 121e69bcfc3SAlexander Langer.Xr abort 3 12262b151c3SJeroen Ruigrok van der Werven.Sh STANDARDS 12362b151c3SJeroen Ruigrok van der WervenThe 12462b151c3SJeroen Ruigrok van der Werven.Fn assert 125cb5f4605SMike Barcroftmacro conforms to 12662b151c3SJeroen Ruigrok van der Werven.St -isoC-99 . 127*33f8d79dSFaraz Vahedi.Pp 128*33f8d79dSFaraz VahediThe 129*33f8d79dSFaraz Vahedi.Fn static_assert 130*33f8d79dSFaraz Vahedimacro conforms to 131*33f8d79dSFaraz Vahedi.St -isoC-2011 . 132afe61c15SRodney W. Grimes.Sh HISTORY 1332038017bSKris KennawayAn 1344b66483fSRuslan Ermilov.Nm 135afe61c15SRodney W. Grimesmacro appeared in 136c40f7c05SWarner Losh.At v7 . 137