How to printf variable?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
UserUserUserUserUser
Posts: 1
Joined: Tue Jan 12, 2021 11:09 am

How to printf variable?

Post 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)
klange
Member
Member
Posts: 679
Joined: Wed Mar 30, 2011 12:31 am
Libera.chat IRC: klange
Discord: klange

Re: How to printf variable?

Post 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.
User avatar
bzt
Member
Member
Posts: 1584
Joined: Thu Oct 13, 2016 4:55 pm
Contact:

Re: How to printf variable?

Post 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
Post Reply