printing out integers, floats

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
shaun

printing out integers, floats

Post by shaun »

I have written a printk function, and I can output text now. I have tried to add the ability to pass variables, int primarily, but everything I have tried the video memory just outputs weird characters. My printk function prototype is
printk(const char*, unsigned int line, char *data);
How should I be passing the variables to my printk function? Because my video_mem is declared as char* I assume anything I assign to video_mem[v] will be printed out as a char, right? Do I have to create a new video memory variable of type int to print out integer variables?
Tim

Re:printing out integers, floats

Post by Tim »

Read up on variable argument lists: the ... keyword and the <stdarg.h> header file.
shaun

Re:printing out integers, floats

Post by shaun »

I can't find anything in stdarg.h that is of any help. I have another question, what does the ... in functions mean?
ie - int   printf (const char*, ...);
If anyone could answer any of these, it would be appreciated.
Tim

Re:printing out integers, floats

Post by Tim »

It's all normal, everyday C code. You need to look these up wherever you learned C originally -- a web board is not the right place to study such fundamental things as the core C language.

Briefly, int printf(const char *fmt, ...) means that printf can take any number of parameters after fmt. printf's code can access these extra parameters using the macros in <stdarg.h>.
lea

Re:printing out integers, floats

Post by lea »

printf(const char *, ...) means you can have any number of args after the const char *. To access the args in your function you need a pointer to the last named arg and just increase it to make it point to the rest of the args. (Usually done with the va_start, va_arg and va_end macros).

To output integers to the screen you have to convert them to a string first.
Post Reply