C庫宏NULL的值是一個(gè)空指針常量。它可以被定義為 ((void*)0), 0 ,0或0L根據(jù)編譯器廠商。
可能是以下聲明為NULL宏取決于編譯器。
#define NULL ((char *)0) or #define NULL 0L or #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("Opend file file.txt successfully "); fclose(fp); } fp = fopen("nofile.txt", "r"); if( fp == NULL ) { printf("Could not open file nofile.txt "); } return(0); }
假設(shè)我們有一個(gè)現(xiàn)有的文件file.txt,和一個(gè)不存在文件nofile.txt。讓我們編譯和運(yùn)行上面的程序,這將產(chǎn)生以下結(jié)果:
Opend file file.txt successfully Could not open file nofile.txt