Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the use of assert function in C language

Shulou Source: shulou.com Published: 2022-06-03 08:58:58 09月20日 Update

Editor to share with you what is the use of the assert function in the c language. I hope you will get something after reading this article. Let's discuss it together.

1. Assert will determine whether the execution program needs to be terminated by checking the value of the expression expresion.

2. If the value of the expression expresion is false (that is, 0), it will first print the error message to the standard error stream stderr.

The program is terminated by calling the abort function. Otherwise, assert has no effect.

Example

# include # include # include int main (void) {FILE * fp; fp = fopen ("456.txt", "w"); / / Open a file in a writable manner, and create a file with the same name assert (fp) if it does not exist; / / so there is no error fclose (fp); fp = fopen ("123.txt", "r") / / Open a file as read-only. If it does not exist, it fails to open the file assert (fp); / / so there is an error fclose (fp); / / the program will never be executed here to return 0 } after reading this article, I believe you have a certain understanding of "what is the use of assert function in c language". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

Tags: Documents functions programs languages methods articles expressions errors messages done examples effects more standards knowledge industry information information channels channels Lilai Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Shulou Tech Info Huawei OPPO Reno Shulou Technology