C 庫宏 - NULL
描述
C 庫宏 NULL 是一個空指針常量的值。它可以被定義為 ((void*)0), 0 或 0L,這取決於編譯器供應商。
聲明
下麵是取決於編譯器的 NULL 宏的聲明。
#define NULL ((char *)0) 或 #define NULL 0L 或 #define NULL 0
參數
- NA
返回值
- NA
實例
下麵的實例演示了 NULL 宏的用法。
#include <stddef.h> #include <stdio.h> int main () { FILE *fp; fp = fopen("file.txt", "r"); if( fp != NULL ) { printf("成功打開檔 file.txt\n"); fclose(fp); } fp = fopen("nofile.txt", "r"); if( fp == NULL ) { printf("不能打開檔 nofile.txt\n"); } return(0); }
假設檔 file.txt 已存在,但是 nofile.txt 不存在。讓我們編譯並運行上面的程式,這將產生以下結果:
成功打開檔 file.txt 不能打開檔 nofile.txt