C
CrazedNerd
Guest
Code:
[email protected]:~/c$ echo hello | wc -c
6
I'm pretty sure the word "hello" has five characters, why does wc say that there are 6 bytes in hello?
[email protected]:~/c$ echo hello | wc -c
6
The echo command adds a trailing newline character. The trailing newline character is counted by the wc command in your example. For the echo command, you can omit the trailing newline character with the "-n" switch.Code:[email protected]:~/c$ echo hello | wc -c 6
I'm pretty sure the word "hello" has five characters, why does wc say that there are 6 bytes in hello?
$ echo hello
hello
$ echo -n hello
hello$ # Observe: No newline before the prompt.
$ echo hello | wc -c
6
$ echo -n hello | wc -c
5
$
That is correct, because when you turn off the newline feature:The echo command adds a trailing newline character. The trailing newline character is counted by the wc command in your example. For the echo command, you can omit the trailing newline character with the "-n" switch.
Code:$ echo hello hello $ echo -n hello hello$ # Observe: No newline before the prompt. $ echo hello | wc -c 6 $ echo -n hello | wc -c 5 $
Hints:
- All of these are different ways to represent the newline character in Unix, Linux, and current Macs:
- \n
- "line feed"
- CTRL-J
- ASCII 10, Hex 0x0A
- "LF"
- In Unix and Linux and any Mac made in the last 20 years, the newline character is a single character.
- -> In Windows, the newline is a sequence of two characters: "carriage return" and then "line feed".
- On very old Macs that run the ancient "classic" MacOS operating system, newline was a single "carriage return" character.
[email protected]:~/c$ echo -n hello | wc -c
5
Is the null character the same thing as a newline?The strings are probably null-terminated and the null character takes a byte.
No, it isn't, but it's the same idea. In C a null character is used to denote the end of a string.Is the null character the same thing as a newline?