OSDev.org

The Place to Start for Operating System Developers
It is currently Wed Apr 24, 2024 8:28 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: How to printf variable?
PostPosted: Tue Jan 12, 2021 11:17 am 
Offline

Joined: Tue Jan 12, 2021 11:09 am
Posts: 1
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)


Top
 Profile  
 
 Post subject: Re: How to printf variable?
PostPosted: Tue Jan 12, 2021 4:52 pm 
Offline
Member
Member

Joined: Wed Mar 30, 2011 12:31 am
Posts: 676
Quote:
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.

_________________
toaruos on github | toaruos.org | gitlab | twitter | bim - a text editor


Top
 Profile  
 
 Post subject: Re: How to printf variable?
PostPosted: Wed Jan 13, 2021 8:19 am 
Offline
Member
Member
User avatar

Joined: Thu Oct 13, 2016 4:55 pm
Posts: 1584
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:
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 156 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group