How to print integers?
How to print integers?
Hi, I have a basic printF function, but it can't print any integers...
Do you know how to convert integer to a string or print integers directly?
Thx!
Do you know how to convert integer to a string or print integers directly?
Thx!
RE:How to print integers?
put them in a int?
void printf(int interger)
{
video[pointer] = interger;
}
void printf(int interger)
{
video[pointer] = interger;
}
RE:How to print integers?
I don't think that will work! video[pointer] = integer is only going to print the ascii corresponding to the truncated value of the integer. Converting integers to string is very easy... All you need to do is use (in C) %,/ and extract each digit of the number and store the corresponding ascii value into a string buffer. Example to extract the least significant number of i = 1654 (which is 4),
buffer[counter] = '0' + (i % 10);
i /= 10;
apply a loop and a few hacks to identify negative numbers, you have an itoa function.
buffer[counter] = '0' + (i % 10);
i /= 10;
apply a loop and a few hacks to identify negative numbers, you have an itoa function.
RE:How to print integers?
It is my experience that the modulous operator be avoided in kernel development, in particular when using gcc.
Example code about the original question:
void convert_to_string(unsigned int x, char *buffer)
{
register unsigned int i = 1000000000;
if (!x)
{
*buffer++ = '0';
*buffer = '\0';
return ;
}
while (i)
{
if ((x / i))
break;
i /= 10;
}
do
{
*buffer++ = '0' + (x / i);
x -= (i * (x / i));
i /= 10;
} while(x);
*buffer = '\0';
}
Use like:
char buffer[256];
convert_to_string(somevalue, buffer);
Example code about the original question:
void convert_to_string(unsigned int x, char *buffer)
{
register unsigned int i = 1000000000;
if (!x)
{
*buffer++ = '0';
*buffer = '\0';
return ;
}
while (i)
{
if ((x / i))
break;
i /= 10;
}
do
{
*buffer++ = '0' + (x / i);
x -= (i * (x / i));
i /= 10;
} while(x);
*buffer = '\0';
}
Use like:
char buffer[256];
convert_to_string(somevalue, buffer);
RE:How to print integers?
Depending upon the host and target system, gcc may replace each instance of the modulous operator with an actual function. That function is generally fpu-friendly. It may either cause a failure in linkage or may cause bad behavior. Personally, I have implemented modulous as a macro to avoid such complications.
RE:How to print integers?
Why would gcc do that ? I mean, the idiv function stores the remainder into %edx register which is the result of the mod operation. Moreover modulus operator has nothing to do with floating point values so why fpu-friendly ?
RE:How to print integers?
Why would gcc do that ? I mean, the idiv function stores the remainder into %edx register which is the result of the mod operation. Moreover modulus operator has nothing to do with floating point values so why fpu-friendly ?
RE:How to print integers?
Actually, it may have a lot to do with the FPU, as gcc often uses the FPU for some optimizations. If you're interested in links where this problem exists:
http://lists.debian.org/debian-powerpc/ ... 01067.html
http://lists.debian.org/debian-glibc/20 ... 00390.html
http://gcc.gnu.org/ml/gcc/2002-08/msg01124.html
http://www.mail-archive.com/rtl@fsmlabs ... 00603.html
...or...
http://www.google.com/search?hl=en&ie=U ... =__umoddi3
and, sure, you can link with libgcc.a, but then there's some licensing issues
http://lists.debian.org/debian-powerpc/ ... 01067.html
http://lists.debian.org/debian-glibc/20 ... 00390.html
http://gcc.gnu.org/ml/gcc/2002-08/msg01124.html
http://www.mail-archive.com/rtl@fsmlabs ... 00603.html
...or...
http://www.google.com/search?hl=en&ie=U ... =__umoddi3
and, sure, you can link with libgcc.a, but then there's some licensing issues
RE:How to print integers?
Interesting! Thanks for the info. Correct me if I'm wrong, but I think these functions are only concerned with data item sizes which the hardware doesn't support (64bit modulo in the case of a 32bit machine, for example when you use "long long"). In case of 32bit data items, I don't think this could pose as a problem, as the hardware provides enough support for a modulo operation. I just "gcc -S" ed a simple program using the modulo op on ints and the assembly code didn't contain any reference to any functions, but on using "long long", it generated reference to __moddi3.
RE:How to print integers?
In most cases, that is the case. However, depending on your platform, you may run into this more often that you might otherwise think.
ISO 9899:1999 5.2.4.2.1 Integers have an implementation defined size.
ISO 9899:1999 6.2.5 (4) long long int is a standard integer type.
ISO 9899:1999 6.3.1.1 long long int must be greater than the rank of long int.
ISO 9899:1999 4-6 freestanding implementations must accept any strictly conforming program, with the exception of complex types.
This means that long long int (per C99) must be available in a freestanding implementation. Of course, earlier versions of gcc never claimed to be C99 compliant, only C89/C90 compliant.
Another thing, on some platforms, gcc will default to optimizing for that particular platform. In the case of Intel, this isn't generally too much of a concern because of a generally poor FPU. However, on other platforms, gcc may even use the FPU for some non-complex numbers.
If you have a prospect for portability, you should definitely take this into consideration.
ISO 9899:1999 5.2.4.2.1 Integers have an implementation defined size.
ISO 9899:1999 6.2.5 (4) long long int is a standard integer type.
ISO 9899:1999 6.3.1.1 long long int must be greater than the rank of long int.
ISO 9899:1999 4-6 freestanding implementations must accept any strictly conforming program, with the exception of complex types.
This means that long long int (per C99) must be available in a freestanding implementation. Of course, earlier versions of gcc never claimed to be C99 compliant, only C89/C90 compliant.
Another thing, on some platforms, gcc will default to optimizing for that particular platform. In the case of Intel, this isn't generally too much of a concern because of a generally poor FPU. However, on other platforms, gcc may even use the FPU for some non-complex numbers.
If you have a prospect for portability, you should definitely take this into consideration.
RE:How to print integers?
Thanks! Could you also tell me how you implemented the modulus operator using macros (as you mentioned before) or point me to resources where I can learn the same ?
Thanks again.
Thanks again.
RE:How to print integers?
But this concersn only gcc, so one sould use a seperate c compiler, which complies with the c99? for each platform.
Anton.
Anton.
RE:How to print integers?
I use gcc myself. Later versions, if they intend to actually become compliant, should fix this problem.