温馨提示×

c语言isempty函数的用法是什么

小亿
112
2023-12-27 23:02:17
栏目: 编程语言

C语言中并没有内置的isempty函数。但可以通过自定义函数来实现类似的功能。

以下是一个示例的自定义函数isEmpty,用于判断一个字符串是否为空:

#include <stdio.h>
#include <stdbool.h>
#include <string.h>

bool isEmpty(const char* str) {
    if (str == NULL) {
        return true;
    }
    if (strlen(str) == 0) {
        return true;
    }
    return false;
}

int main() {
    char str1[] = "";  // 空字符串
    char str2[] = "Hello";  // 非空字符串

    if (isEmpty(str1)) {
        printf("str1 is empty\n");
    } else {
        printf("str1 is not empty\n");
    }

    if (isEmpty(str2)) {
        printf("str2 is empty\n");
    } else {
        printf("str2 is not empty\n");
    }

    return 0;
}

运行以上代码,输出结果为:

str1 is empty
str2 is not empty

需要注意的是,以上自定义的isEmpty函数只能判断字符串是否为空,无法判断其他类型的变量是否为空。

0