I was trying to understand write function and its capabilities, I tried to write a function that gives the output of 5 since 5*10 is 50 but I could only write 1 byte I assumed that the output would be 5. Why is it 2?
#include <unistd.h>
void ft_putchar(int c){
write(1, &c, 1);
}
int main(){
ft_putchar(5 * 10);
}
CodePudding user response:
5 * 10
is 50
. The character code 50
corresponds to the character 2
in ASCII. Therefore the output is 2
when interpreted as ASCII (or character code compatible to ASCII, such as UTF-8).
Also note that int
has typically 4 (or 2) bytes. It looks like the first byte, which is written via the write
function, contained the value 50
because it is less than 256 and you are using a little-endian machine.