I have this small C program (play.c):
#include <stdio.h>
main() {
int t;
for (t=0; t<8000; t++) {
printf ("%c", t%256);
}
}
when I redirect it’s output (in Linux):
./play > /dev/audio
it plays a sound as I expect. But if I format the output in printf as int (%d) instead of char, I get a different sound (and a wrong one, as far as my ears are telling me).
Why does that happen? Aren’t the same numbers are sent to the audio device?
No, as
man printfwould tell you, or a simple diff on the output redirected into two files. ‘%c’ prints the unsigned char part (the first 8 bits) as one character, and ‘%d’ the whole number in base-10 digit representation.