control character whose bits are all 0
Videos
Hello everyone!
In C, strings (character arrays) are terminated by null character '\0' - character with value zero.
In ASCII, the NUL control code has value 0 (0x00). Now, if we were working in different character set (say the machine's character set wouldn't be ASCII but different one), should the strings be terminated by NUL in that character set, or by a character whose value is zero?
For example, if the machine's character set would be UTF-16, the in C, byte would be 16bits and strings would be terminated by \0 character with value 0x00 00, which is also NUL in UTF-16.
But, what if the machine's character set would be modified UTF-8 (or UTF-7, ...). Then, according to Wikipedia, the null character is encoded as two bytes 0xC0, 0x80. How would be strings terminated in that case? By the byte with value 0 or by the null character.
I guess my question could be rephrased as: Are null terminated strings terminated by the NUL character (which in that character set might be represented by a nonzero value) or by a character whose value is zero (which in that character set might not represent the NUL character).
Thank you all very much and I'm sorry for all mistakes and errors as english is not my first language.
Thanks again.
The ASCII character '0' is different than the number 0. You are printing the integer 0 in your second pair of printfs instead of '0'.
Try this:
printf("'\\0' : %d\n", '\0');
printf("'\\0' in hex : %x\n", '\0');
printf("'0' : %d\n", '0');
printf("'0' in hex: %x\n", '0');
Also, you don't need to escape ' inside strings. That is, "'" is fine and you don't need to write "\'"
You confuse 0, '\0', and '0'.
The first two of these are the same thing; they just represent an int with value 0.
'0', however, is different, and represents an int with the value of the '0' character, which is 48.
So, it's represented by '\0' or '0', right?
Why '0' too when it's value is 48 and not 0 - is it not the character '0' in ASCII but the symbol '0' used when talking about the NULL character?
EDIT: So it's just the ASCII character '\0'.
ALSO: Why is a char array printable as a string even if it contains no NULL character?
#include <stdio.h>
int main(void)
{
char s[] = {'h', 'i', '.', '.'};
printf("%s\n", s);
printf("%i %i %i %i\n", s[0], s[1], s[2], s[3]);
}prints
hi.. 104 105 46 46
but if I change the last line to
printf("%i %i %i %i %i\n", s[0], s[1], s[2], s[3], s[4);it complains "array index 4 is past the end of the array" which means there is no NULL.
EDIT: I just used the code above and changed s[] to s[5] and it printed the additional 0 - why - can't printf see the NULL if you declare the array without specifying its size in advance?