4 ms·
Here it is, pulled out of the linked page: main( ) { extrn a, b, c; putchar(a); putchar(b); putchar(c); putchar('!*n'); } a 'hell';
by quietbritishjim 5y ago
Here it is, pulled out of the linked page:
main( ) {
extrn a, b, c;
putchar(a); putchar(b); putchar(c); putchar('!*n');
}
a 'hell';
b 'o, w';
c 'orld';
B uses single quotes to denote a character (like C), and double quotes to denote a string. Each character is a word (as are all variables in B), which is 36 bits long, so it can hold 4 ASCII characters! A character literal with fewer than 4 characters is zero padded (as is, presumably, the left over 4-bit nibble). In fact an earlier snippet in that document just outputs "Hi!" because that way you only need one character:
main( ) {
auto a;
a= 'hi!';
putchar(a);
putchar('*n' );
}
- kazinator 5y agoIn C, the character constant is still of type int, not char like in C++. Multi-character constants are possible, and have an implementation-defined value. int fourcc = 'abcd'; (This may be supported by C++ also, I'm not sure. So that is to say, a multi-character constant in C++ perhaps doesn't have type char, but an implementation-defined type.) GCC on Ubuntu 18.04: $ cat hello.c #include <stdio.h> int main(void) { int hello[] = { 'lleH', 'w ,o', 'dlro', '!', 0 }; puts((char *) hello); return 0; } $ gcc hello.c -o hello hello.c: In function ‘main’: hello.c:5:19: warning: multi-character character constant [-Wmultichar] int hello[] = { 'lleH', 'w ,o', 'dlro', '!', 0 }; ^~~~~~ [ ... and similar errors ...] $ ./hello Hello, world! It works if the program is compiled with g++ also, in spite of a single character constant like '!' being char. We have to write the characters backwards because of little endian. In the source code, the leftmost character is the most significant byte, and on the little-endian system, that means it goes to the higher address. The first character H must be the rightmost part of the character constant so that it ends up in the least significant byte which goes to the lowest address in memory. Endianness wouldn't be an issue in B because it doesn't exist; there is no "char *" pointer accessing the data as individual characters. B could be implemented on big or little endian and the string handling would work right.