-2

i think that every string array ends with a NULL charcter but in thiss program the statment of checking if the last charcter of the string array is not True

#include <stdlib.h>
#include <stdio.h>


int main() {

char hey[5]="hello";


if(hey[5] == NULL){
    puts("Yes the last charcter is a NULL ");
}

return 0;

}

and also if NULL is \0 why when i try if (NULL == "\0") the statement is always False ?

1 Answers1

0
  1. Your string is too short and the initialization copies only 5 chars. The hey[5] (6th element of the array) is past the array bounds and is not determined
char hey[]="hello";

if(hey[5] == '\0'  /* or simply 0 */){
    puts("Yes the last character is a nul terminator");
}
  1. NULL is not a nul terminator.
0___________
  • 60,014
  • 4
  • 34
  • 74