
- C Library - Home
- C Library - <assert.h>
- C Library - <complex.h>
- C Library - <ctype.h>
- C Library - <errno.h>
- C Library - <fenv.h>
- C Library - <float.h>
- C Library - <inttypes.h>
- C Library - <iso646.h>
- C Library - <limits.h>
- C Library - <locale.h>
- C Library - <math.h>
- C Library - <setjmp.h>
- C Library - <signal.h>
- C Library - <stdalign.h>
- C Library - <stdarg.h>
- C Library - <stdbool.h>
- C Library - <stddef.h>
- C Library - <stdio.h>
- C Library - <stdlib.h>
- C Library - <string.h>
- C Library - <tgmath.h>
- C Library - <time.h>
- C Library - <wctype.h>
- C Programming Resources
- C Programming - Tutorial
- C - Useful Resources
C library - NULL Macro
The C library NULL Macro represent the value of a null pointer constant that may be defined as ((void*)0), 0 or 0L depending on the compiler vendor.
Following is the list of NULL macro structure −
- NULL (void*)0: A null pointer cast to the void* type.
- NULL 0: An integer literal representing a null pointer.
- NULL 0L: A long integer literal representing a null pointer.
Syntax
Following is the C library syntax of the NULL Macro.
#define NULL ((char *)0) or, #define NULL 0L or #define NULL 0
Parameters
- This is not a function. So, it doesn't accept any parameter.
Return Value
This macro doesn't return any value.
Example 1
Following is the basic C library macro NULL Macro to see its demonstration on file handling.
#include <stddef.h> #include <stdio.h> int main () { FILE *fp; fp = fopen("file.txt", "r"); if( fp != NULL ) { printf("Opend file file.txt successfully\n"); fclose(fp); } fp = fopen("nofile.txt", "r"); if( fp == NULL ) { printf("Could not open file nofile.txt\n"); } return(0); }
Output
Assume that we have an existing file file.txt but nofile.txt does not exist. By compiling the above program, we get the following result −
Opend file file.txt successfully Could not open file nofile.txt
Example 2
Below the given program shows the usage of memory allocation error using errno(macro).
#include <stdio.h> #include <stdlib.h> #include <errno.h> #include <string.h> int main() { size_t size = 100000000000; int *arr = malloc(size * sizeof(int)); if (arr == NULL) { fprintf(stderr, "Memory allocation failed: %s\n", strerror(errno)); } else { printf("Memory allocated successfully!\n"); free(arr); } return 0; }
Output
On execution of above code, we get the following result −
Memory allocation failed: Cannot allocate memory
Advertisements