1
char word[5]={"fayed"};

word[1]=NUL;

printf("%s",word);

It shows error ... and NUL to be undeclared.

Esterified
  • 63
  • 1
  • 2
  • 11
  • No, `NUL` is not a thing. – Oliver Charlesworth Jan 24 '15 at 11:14
  • It is a pretty commonly available #define for '\0'. Probably to help programmers distinguish 0 from ASCII '0'. It is not standard, best not to rely on it. – Hans Passant Jan 24 '15 at 11:52
  • Codeblocks is not a compiler. – Fernando Jan 25 '15 at 04:51
  • yes it is not.i want to say about the library that this IDE contains. – Esterified Jan 25 '15 at 07:35
  • It is just an IDE, the headers that you use are not part of Codeblocks, these are part of several different libraries. If you are using the Windows version of Codeblocks they are shipped with it as part of MinGW, in other systems either they come with the system or are installed as you install new libraries. It is not to be pedantic, it is important you know, the IDE is irrelevant in this question. – Fernando Jan 25 '15 at 08:23

2 Answers2

3

No, there is no standard identifier called NUL. If you want to null-terminate a string, use 0 or '\0'.

For completeness, it is worth noting that the null pointer is spelt NULL. See If NULL and 0 are equivalent as null pointer constants, which should I use?

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • I am pretty sure the question is about NUL, the ASCII character, and that any reference to NULL, the pointer is tangential. – Pascal Cuoq Jan 24 '15 at 11:21
  • 1
    @PascalCuoq: Absolutely. `NULL` was there for completeness. I've reworded the answer to make it clear. – NPE Jan 24 '15 at 13:20
3

You are referring to NUL, the ASCII character.

The C standard does not mandate ASCII encoding for characters, but it does use the character value 0 to terminate strings (one common meaning of NUL in ASCII).

Just use 0:

word[1]=0;

And make sure not to confuse it with the character '0'.

Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281
  • 1
    I feel when assigning to a `char` using `'\0'` instead of `0` is nicer. – alk Jan 24 '15 at 11:27
  • @akj me too. *"Use '\0' instead of 0 to represent the null byte at the end of a string."*, Kernighan & Pike in The practice of programming – ouah Jan 24 '15 at 11:53