My guess is he wants to print out the binary form of each character. Normally, you could print that using a printf format, but the C standard doesn't require such a format conversion. Here's how to do it.
b.c:
Code:
#include <stdio.h>
void
print_string_in_binary(const char *str)
{
int i;
while (*str)
{
printf("%d-", *str);
for (i = 6;i >= 0;i--)
{
printf("%d", *str >> i & 0x1);
}
printf(" ");
str++;
}
printf("\n");
}
int
main(void)
{
const char *s = "hello world\n";
printf("s = %s\n", s);
print_string_in_binary(s);
return 0;
}
The key is to print out each bit of a character by shifting it to the least significant bit and bitwise ANDing the resulting integer with 1, which is the maximum binary value. That's what this does: "*str >> i & 0x1". Those of you not well versed with pointers should try to think of *str as str[i] with i being incremented whenever you increment str (note the "i" used here has no relation to the "i" in the print_string_in_binary function).
Result:
Code:
puck% gcc -Wall -o b b.c && ./b
s = hello world
104-1101000 101-1100101 108-1101100 108-1101100 111-1101111 32-0100000 119-1110111 111-1101111 114-1110010 108-1101100 100-1100100 10-0001010
A quick check indicates that this is the correct output.