0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

C言語のNULLの正体は0なのか?

Posted at

初めに

C言語で頻繁に使われるNULL。
「何もない」を意味しますが、実際には何らかの値であるはずです。
よく「NULLの正体は0」だと言われますが、本当にそうなのでしょうか?
実際にコードを書いて確認してみました。

実験用プログラム

demo.c
#include <stdio.h>
#include <stddef.h>

int main() {

    if (NULL == 0) {
        printf("NULL is equal to 0\n");
    } else {
        printf("NULL is not equal to 0\n");
    }

    printf("NULL (as integer): %d\n", NULL);
    printf("0 (as integer): %d\n", 0);

    return 0;
}

実行結果

NULL == 0 が真であり、整数として表示しても 0。
つまり、この環境ではNULL0と同じだと確認できました。

C:\Users\nanasi\test3>gcc demo.c

C:\Users\nanasi\test3>a
NULL is equal to 0
NULL (as integer): 0
0 (as integer): 0

定義位置

定義されている場所を調べましたがNULLに関するものは以下の1行だけで見つけられませんでした。

\mingw64\include\c++\14.2.0\cstdlib
#define NULL __null

環境

Windows10 (10.0.19045.5737)
gcc version 14.2.0

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?