C 库宏 - assert()

C 语言教程 · 2019-02-22 19:12:04

描述

C 库宏 void assert(int expression) 允许诊断信息被写入到标准错误文件中。换句话说,它可用于在 C 程序中添加诊断。

声明

下面是 assert() 宏的声明。

void assert(int expression);

参数

  • expression -- 这可以是一个变量或任何 C 表达式。如果 expression 为 TRUE,assert() 不执行任何动作。如果 expression 为 FALSE,assert() 会在标准错误 stderr 上显示错误消息,并中止程序执行。

返回值

这个宏不返回任何值。

实例

下面的实例演示了 assert() 宏的用法。

实例

#include <assert.h> #include <stdio.h> int main() { int a; char str[50]; printf("请输入一个整数值: "); scanf("%d", &a); assert(a >= 10); printf("输入的整数是: %d\n", a); printf("请输入字符串: "); scanf("%s", str); assert(str != NULL); printf("输入的字符串是: %s\n", str); return(0); }

让我们在交互模式下编译并运行上面的程序,如下所示:

请输入一个整数值: 11
输入的整数是: 11
请输入字符串: codercto 
输入的字符串是: codercto 

点击查看所有 C 语言教程 文章: https://codercto.com/courses/l/17.html

查看所有标签

The Elements of Statistical Learning

The Elements of Statistical Learning

Trevor Hastie、Robert Tibshirani、Jerome Friedman / Springer / 2009-10-1 / GBP 62.99

During the past decade there has been an explosion in computation and information technology. With it have come vast amounts of data in a variety of fields such as medicine, biology, finance, and mark......一起来看看 《The Elements of Statistical Learning》 这本书的介绍吧!

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具