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://www.codercto.com/courses/l/17.html

查看所有标签

Letting Go of the Words

Letting Go of the Words

Janice (Ginny) Redish / Morgan Kaufmann / 2007-06-11 / USD 49.95

"Redish has done her homework and created a thorough overview of the issues in writing for the Web. Ironically, I must recommend that you read her every word so that you can find out why your customer......一起来看看 《Letting Go of the Words》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

SHA 加密
SHA 加密

SHA 加密工具