C言語のisempty関数の使い方は何ですか?

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関数は、文字列が空かどうかしか判断できず、他のタイプの変数が空かどうかは判断できないことに注意してください。

bannerAds