Code:
int fd;
fd = open("fruit", O_WRONLY);
write(fd, "apple", sizeof("apple"));
close(fd);
I compile it with
$ gcc test.c -o test
and run as
$ ./test
Then I open the fruit
file, and I see the following in the file:
apple^@
What does the ^@
mean?
Normally it means the user's home directory e.g. ~mike/ would be the user mike 's home directory, ~/ would be your own home directory.
From Wikipedia: “The tilde symbol is used to prefix hidden temporary files that are created when a document is opened in Windows. For example, when you open a Word document called “Document1. doc,” a file called “~$cument1. doc” is created in the same directory.
means it's the current directory. / is normally used in paths to show the structure of files and folders. \\ is referring to \ (used double because \ is a special character) which is same as / but used differently depending on the OS.
A relative path refers to a location that is relative to a current directory. Relative paths make use of two special symbols, a dot (.) and a double-dot (..), which translate into the current directory and the parent directory. Double dots are used for moving up in the hierarchy.
It is the null character code '\0'
. Certain editors like vi display it as ^@
.
sizeof("apple")
would return 6 because it includes the null character used to terminate strings.
The ^@
is the way an ASCII NUL is commonly represented in printable form. That is the same as the @
character with some bits removed:
@ = 0100
^@ = 0
and it is the same as '\0'
(the string terminator in C). Because it is the string terminator, you would not see it from printf
or its related functions, but you can readily create it using the block-oriented write
. For example, you could have written
write(fd,"apple\0orange",sizeof("apple\0orange"));
and seen
apple^@orange^@
because every double-quoted literal in C has a trailing string terminator which is counted in its size. If you had meant to write the string without its terminator, you could have done this:
const char *s = "apple";
write(fd,s,strlen(s));
thereby eliminating two problems in the example: (a) incorrect length and (b) possibly using inconsistent string content and length by ensuring both are the same item. See Sizeof string literal for some comments on (a).
NUL
is one of the 32 ASCII control characters whose values range from 0 to 31, called C0 controls.
All of these ASCII control characters are typically shown in this way (for a printable form), using the character corresponding to adding 64 (0100) to the control character's value.
ASCII DEL
is 127 (0177). Displaying it as ^?
is a special case which is more recent than the other ASCII control characters. For instance, X/Open terminfo (curses) does not define a printable form for this character, although it does for the others. Unlike the other ASCII control characters, DEL
is formed by OR'ing all (seven) bits into the character.
ASCII is a 7-bit code, of course. Many other codes were developed; ASCII corresponds to the POSIX portable character set, so it is frequently encountered.
It is easy to find tables of ASCII characters with a web search. Most of these tables (or their accompanying discussion) veer off into misinformation. Here is a link to a reasonably factual page, entitled ASCII Character Set. It states
The Control key subtracts 64 from the value of the keys that it modifies.
However, the statement is only correct if the key is one of those from the set @
, A
, B
, etc. If you apply that to other keys, the results are perhaps interesting but not useful. Rather, in a C program you would do a logical masking, e.g.,
ch = ch & 037;
to obtain a character in the range 0 to 31.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With