
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is a Simple Assertion in C Language
An assertion is a statement which is used to declare positively that a fact must be true when that line of code is reached.
Assertions are useful for obtaining the expected conditions which are met.
Simple Assertion
Simple assertion can be implemented by using assert (expression) method, which is present in assert.h header file.
The syntax for simple assertion is as follows −
assert(expression)
In simple assertion,
- When the condition passed to an assertion which is a true, then, there is no action.
- The behaviour on false statements depends completely on compiler flags.
- When assertions are enabled, a false input causes a program to halt.
- When assertions are disabled, then, there is no action.
Assertions are used only to catch the internal programming errors. These errors occur by passing the bad parameters.
Example
Following is the C program for simple assertion in C programming language −
#include <stdio.h> #include <assert.h> int main(void){ int x; printf("Enter the value of x:
"); scanf("%d",&x); assert(x >= 0); printf("x = %d
", x); return 0; }
Output
When the above program is executed, it produces the following output −
Run 1: Enter the value of x: 20 x = 20 Run 2: Enter the value of x: -3 Assertion failed! Program: G:\CP\CP programs\test.exe File: G:\CP\CP programs\test.c, Line 10 Expression: x >= 0
Advertisements