c - Difference between binary zeros and ASCII character zero -
gcc (gcc) 4.8.1 c89
hello,
i reading book pointers. , using code sample:
memset(buffer, 0, sizeof buffer);
will fill buffer binary 0 , not character zero.
i wondering difference between binary , character zero. thought same thing.
i know textual data human readable characters , binary data non-printable characters. correct me if wrong.
what example of binary data?
for added example, if dealing strings (textual data) should use fprintf
. , if using binary data should use fwrite
. if want write data file.
many suggestions,
the quick answer character '0'
represented in binary data ascii number 48. means, when want character '0'
, file has these bits in it: 00110000
. similarly, printable character '1'
has decimal value of 49, , represented byte 00110001
. ('a'
65, , represented 01000001
, while 'a'
97, , represented 01100001
.)
if want null terminator @ end of string, '\0'
, has 0 decimal value, , byte of zeroes: 00000000
. 0 value. compiler, there no difference between
memset(buffer, 0, sizeof buffer);
and
memset(buffer, '\0', sizeof buffer);
the difference semantic 1 us. '\0'
tells we're dealing character, while 0 tells we're dealing number.
it tremendously check out ascii table.
fprintf
outputs data using ascii , outputs strings. fwrite
writes pure binary data. if fprintf(fp, "0")
, put value 48 in fp, while if fwrite(fd, 0)
put actual value of 0 in file. (note, usage of fprintf
, fwrite
not proper usage, shows point.)
note: answer refers ascii because it's 1 of oldest, best known character sets, eric postpichil mentions in comments, c standard isn't bound ascii. (in fact, while give examples using ascii, standard seems go out of way never assume ascii character set used.). fprintf
outputs using execution character set of compiled program.
Comments
Post a Comment