Page 1 of 1
How to printf variable?
Posted: Tue Jan 12, 2021 11:17 am
by UserUserUserUserUser
I am using printf from Meaty Skeleton (
https://wiki.osdev.org/Meaty_Skeleton). After some changes of terminal_putchar, it works fine, when I'm writing ready string (printf("Hello\n") gives me "Hello" output), but when I use variable (printf(a), where's a is char), it gives me random symbols. How to make it say variable value? (I am using i686-elf-gcc)
Re: How to printf variable?
Posted: Tue Jan 12, 2021 4:52 pm
by klange
but when I use variable (printf(a), where's a is char),
It sounds like you're trying to pass a char as an argument to a function that takes a pointer. At the risk of sounding harsh, this is a very basic C thing and if you don't see how this is wrong you'll have a difficult time moving forward with OS dev.
Re: How to printf variable?
Posted: Wed Jan 13, 2021 8:19 am
by bzt
UserUserUserUserUser wrote:when I use variable (printf(a), where's a is char), it gives me random symbols.
The first argument must be a zero terminated string which specifies the formating. Are you sure the compiler didn't gave you a warning about using "printf(a)"?
UserUserUserUserUser wrote:How to make it say variable value?
Depends on the variable.
Code: Select all
printf("%c\n", a); // print char
printf("%d\n", i); // print integer in decimal
printf("%x\n", i); // print integer in hexadecimal
printf("%s\n", s); // print another string
... etc.
See section "Conversion specifiers" in
man printf.
klange wrote:At the risk of sounding harsh, this is a very basic C thing and if you don't see how this is wrong you'll have a difficult time moving forward with OS dev.
I don't want to be harsh either, but klange is right.
Cheers,
bzt