Page 1 of 1

Value as string?

Posted: Fri Feb 06, 2004 6:59 pm
by St8ic
Hi

When I do a printf(1); it displays gibberish, but when I do printf("1"); it actualy displays 1. How can I display the value 1 on the screen, without having it being a string?

Thanks

Re:Value as string?

Posted: Fri Feb 06, 2004 7:11 pm
by Tim
printf("%d", 1);

printf(1) is illegal. You should have seen a compiler error or a crash instead of gibberish.

Re:Value as string?

Posted: Sat Feb 07, 2004 8:22 am
by Therx
No surely it will just use memory address 1 as the format string.

Re:Value as string?

Posted: Sat Feb 07, 2004 11:25 am
by Schol-R-LEA
Pete wrote: No surely it will just use memory address 1 as the format string.
Possibly, assuming that that address is valid; there would be an implicit cast from int to char*, I think, and depending on the compiler, it might not even give a warning. Very nasty, that. I wrote a test program (C, not C++) which I compiled under both Dev-C++ (gcc 3.2.2) and Visual C++, and both gave warnings about the implicit cast. I had all warnings set for both compilers.

There's a good chance it would cause a segfault or GPF, though, from invalid memory access - but then, since it is only reading the memory and not writing to it, it may not. In the case of my test program, it crashed under both compilers, which was what I anticipated - reading address 1 is never valid under Windows XP, AFAIK. I don't know if Linux permits it; I know a lot of old Unix code assumes that you can read address 0, which Linux allows for the sake of backwards compatibility, but I don't know about address 1. I'll check next time I'm running in my Linux partition (I'm intending to rebuild it from scratch, so it may not be any time soon).

In C++, this would be an error, rather than just a warning; when I compiled the test program as a C++ souce file rather than C, both compilers reported it as an invalid cast error.

Re:Value as string?

Posted: Sat Feb 07, 2004 11:43 am
by St8ic
For Tim's code it just prints

%d

No errors. I suppose I need some type of support in my printf function to use this format...

printf() considered harmful

Posted: Sat Feb 07, 2004 11:49 am
by Schol-R-LEA
<soapbox>
I know it's a standard function and used for most console I/O, but I've never felt comfortable with [tt]printf()[/tt]. Not only does it have all kinds of potential type-violation problems, it isn't very efficient - since you already know the type of the values to be printed, why have the program waste time parsing the format string? IMAO, I would rather have separate functions for printing ints and floats, as in Modula-2 and Ada, or have to explicitly convert them to strings, as in Java. I've always seen the heavy use of [tt]printf()[/tt] as something of a problem in C - while it has a purpose, esp. with complex formatting, in most cases it is overkill. I tend to use [tt]puts()[/tt] for simple strings instead, and often use [tt]itoa()[/tt] and [tt]puts()[/tt] for ints (though that can be a pain, as you need to have a buffer reserved for [tt]itoa()[/tt]); I reserve [tt]printf()[/tt] for specifying floating-point precisions, or for cases where I'm printing several values which have to be precisely aligned in regard to each other.
</soapbox>

Re:Value as string?

Posted: Sun Feb 08, 2004 9:30 am
by Pype.Clicker
** please ** do not call your kernel-dedicated display function "printf" ... it will confuse everyone. Call it kprint, kprintf, printk, whatever ... but leave the standard-defined library names for standard libraries ...

indeed, your kprint function will not magically convert integers into strings ... the easiest to display is the hex numbers: you have a number in X and select the (x&15)th character of "0123456789abcdef" as the character to be displayed, then you shift your number to the right by 4 and repeat the operation until the number's value is 0 ...