Printf

H

hello123

Hi,

Can i ask the question:

If you have an integer(int), how to print it out without using printf?

Thanks
 
A

Allan Bruce

hello123 said:
Hi,

Can i ask the question:

If you have an integer(int), how to print it out without using printf?

Thanks

by getting a book and reading about C. If you have to ask this then you
will have a lot of trouble with C.

here is the most basic way of printing out an integer using printf, there
are howver many other things that can be taken into consideration, e.g.
formatting.

printf("Integer = %d", myInt);

Allan
 
O

osmium

hello123

Hello, hello:
Can i ask the question:

If you have an integer(int), how to print it out without using printf?

By using the API for your OS. Just as a wild guess you might get an answer
on a group with something like ms-dos in its name.

Resolve the gibberish here:

http://www.acronymfinder.com/
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi,

Can i ask the question:

If you have an integer(int), how to print it out without using printf?

This looks like a "Programming class" challange/exercise to me.

The solution depends on how strict that "without using printf" directive is.
Strictly speaking, sprintf() isn't printf(). So, you could use sprintf() to
convert the integer into a printable string, then print it using puts() or a
putchar() loop.

However, if you aren't permitted to use sprintf(), you /can/ write your own
integer-to-string conversion function, and use it instead. If you do so, you'll
have to be sensitive to the sign of the source integer, and to handling of
signed integers in general. You'll want something like

char *IntToString(int number, char *string)
{
if (number > 10) /* take care of leading digits */
string = IntToString(number/10,string);
*string++ = '0' + (number%10);
*string = '\0';

return string;
}

but with logic to properly manage negative numbers.

- --
Lew Pitcher
IT Consultant, Enterprise Application Architecture,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed are my own, not my employers')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFAh+PMagVFX4UWr64RAoY9AJ4170vcYACQj5U2gd+t99eDDHFinACg2PdC
QXnrszOfTBKdphrMG9q+ehY=
=qVa4
-----END PGP SIGNATURE-----
 
A

Allan Bruce

Christopher Benson-Manica said:
I think you'll want to reread OP's question.

ah *without*. The title should have been a bit more of a clue!

well, there are several ways to do this. I would suggest taking your number
and successively dividing by 10, and storing the results in a char array.
You can do

char a = 3 + '0';
putc(a);
fflush(stdout);

which will print out the number 3.

(Of course, a cheating way would be to us sprintf() but I guess you mean to
not use it either.)
Allan
 
R

Robert B. Clark

Can i ask the question:

If you have an integer(int), how to print it out without using printf?

There are a number of methods that come to mind, but they are all decidedly
platform-specific and some are downright nasty.

The question is, why do you want to do this? Your requirements as stated
could be met to the letter by simply using vprintf or sprintf. Would that
be sufficient, or would you lose marks for excessive cheesiness? :)

#include <stdio.h>

int main(void)
{
int x = 47;
char s[10];

sprintf(s, "%d", x);
puts(s);

return 0;
}

Not terribly useful, but it is portable and does not use printf.
 
S

Sam Dennis

Robert said:
There are a number of methods that come to mind, but they are all decidedly
platform-specific and some are downright nasty.

As there's nothing inherently unportable about either (f)putc or the
necessary arithmetic, one has to wonder how you thought it should be
done; note that the standard guarantees the presence, contiguity and
order of the decimal digits in the execution character set. (Except
for the first requirement, it'd still be easy enough.)
 
P

pete

hello123 said:
Hi,

Can i ask the question:

If you have an integer(int), how to print it out without using printf?

/* BEGIN integer.c */

#include <stdio.h>

#define INTEGER -1230

#define put_c(c, stream) (putc((c), (stream)))
#define put_char(c) (put_c((c), stdout))

int fsput_d(int);
int fsput_u(unsigned);
int fsput_u_plus_1(unsigned);

int main(void)
{
int integer = INTEGER;

fsput_d(integer);
put_char('\n');
return 0;
}

int fsput_u(unsigned integer)
{
int count;
unsigned digit, tenth;

tenth = integer / 10;
digit = integer - 10 * tenth + '0';
count = tenth != 0 ? fsput_u(tenth) : 0;
return count != EOF && put_char(digit) != EOF ? count + 1 : EOF;
}

int fsput_u_plus_1(unsigned integer)
{
int count;
unsigned digit, tenth;

tenth = integer / 10;
digit = integer - 10 * tenth + '0';
if (digit == '9') {
if (tenth != 0) {
count = fsput_u_plus_1(tenth);
} else {
count = put_char('1') == EOF ? EOF : 1;
}
digit = '0';
} else {
count = tenth != 0 ? fsput_u(tenth) : 0;
++digit;
}
return count != EOF && put_char(digit) != EOF ? count + 1 : EOF;
}

int fsput_d(int integer)
{
int count;

if (0 > integer) {
if (put_char('-') == EOF) {
return EOF;
}
count = fsput_u_plus_1(-(integer + 1));
return count == EOF ? EOF : count + 1;
} else {
return fsput_u(integer);
}
}

/* END integer.c */
 
A

Alex

pete said:
hello123 said:
If you have an integer(int), how to print it out without using printf?

/* BEGIN integer.c */ [snip]
int fsput_u(unsigned integer) [snip]
int fsput_u_plus_1(unsigned integer) [snip]
int fsput_d(int integer)
{
int count;

if (0 > integer) {
if (put_char('-') == EOF) {
return EOF;
}
count = fsput_u_plus_1(-(integer + 1));
return count == EOF ? EOF : count + 1;
} else {
return fsput_u(integer);
}
}

/* END integer.c */

I guess fsput_u_plus_1() is because -INT_MIN may be greater than INT_MAX?

How about:

int fsput_u(unsigned n)
{
int count = 0;
int c = '0' + (n % 10);
n /= 10;
if (n && (count = fsput_u(n)) == EOF) return EOF;
return (put_char(c) == EOF) ? EOF : (count + 1);
}

int fsput_d(int n)
{
if (n < 0) {
int count, units;

if (put_char('-') == EOF) return EOF;
units = n % 10;
n /= 10;
if (units < 0) units = -units;
count = 0;
if (n && (count = fsput_u(-n)) == EOF) return EOF;
return (put_char('0' + units) == EOF) ? EOF : (count + 2);
}
return fsput_u(n);
}
 
C

CBFalconer

pete said:
/* BEGIN integer.c */

#include <stdio.h>
.... snip code ...

Piggybacking. Try:

#include <stdio.h>

/* Return EOF for error */
int putword(unsigned long w, FILE *f)
{
if (w > 9)
if (0 > putword(w / 10, f)) return EOF;
return putc((w % 10) + '0', f);
} /* putword */

/* --------------- */

#ifdef TESTING

#include <stdlib.h>

int main(void)
{
int i;

for (i = 0; i < 10; i++) {
putword(i, stdout);
putc(' ', stdout);
putword(rand(), stdout);
putc('\n', stdout);
}
return 0;
} /* main */
#endif
 
J

Joe Wright

CBFalconer said:
... snip code ...

Piggybacking. Try:

#include <stdio.h>

/* Return EOF for error */
int putword(unsigned long w, FILE *f)
{
if (w > 9)
if (0 > putword(w / 10, f)) return EOF;
return putc((w % 10) + '0', f);
} /* putword */

/* --------------- */

#ifdef TESTING

#include <stdlib.h>

int main(void)
{
int i;

for (i = 0; i < 10; i++) {
putword(i, stdout);
putc(' ', stdout);
putword(rand(), stdout);
putc('\n', stdout);
}
return 0;
} /* main */
#endif
Does everybody have their tongues in cheek?

The question is 'How can I do itoa()?' and is asked and answered in
virtually every C text I have seen. The answer is sometimes a
demonstration of simple recursion.
 
C

CBFalconer

Joe said:
Does everybody have their tongues in cheek?

The question is 'How can I do itoa()?' and is asked and answered
in virtually every C text I have seen. The answer is sometimes a
demonstration of simple recursion.

The above is in no sense an implementation of itoa, nor of
sprintf. It may well be a routine called by sprintf. The
essential difference is that putword() above deals only with an
output stream, there are no strings involved, and thus no buffers
to size and/or overflow.

<rant>
People seem to feel that all input and output has to be done with
strings and buffers, or sometimes with scanf and friends (which
are hard for anybody other than Dan Pop to use correctly). Yet a
few simple i/o routines can do all this work with streams, and
never run into a buffer overflow, or fail to know where the error
lies.
</rant>
 
P

pete

Alex said:
pete said:
hello123 said:
If you have an integer(int),
how to print it out without using printf?

/* BEGIN integer.c */ [snip]
int fsput_u(unsigned integer) [snip]
int fsput_u_plus_1(unsigned integer) [snip]
int fsput_d(int integer)
{
int count;

if (0 > integer) {
if (put_char('-') == EOF) {
return EOF;
}
count = fsput_u_plus_1(-(integer + 1));
return count == EOF ? EOF : count + 1;
} else {
return fsput_u(integer);
}
}

/* END integer.c */

I guess fsput_u_plus_1()
is because -INT_MIN may be greater than INT_MAX?
Yes.


How about:
int fsput_d(int n)
{
if (n < 0) {
int count, units;

if (put_char('-') == EOF) return EOF;
units = n % 10;

If n is negative, then you have to do something tricky
like "if (-1 % 2 < 0)", because the result of integer
division on negative values, is implementation defined.

Note the "if (-1 % 2 < 0)" in Peter Nilsson's itoa:

http://groups.google.com/[email protected]
 
A

Alex Fraser

[snip]
If n is negative, then you have to do something tricky
like "if (-1 % 2 < 0)", because the result of integer
division on negative values, is implementation defined.

Note the "if (-1 % 2 < 0)" in Peter Nilsson's itoa:

http://groups.google.com/[email protected]

Interesting post, and thread. My interpretation of N869 is that integer
division is defined to truncate towards zero and therefore, for "(a/b)*b +
a%b == a" to be true, a%b must have the sign of a. I can only assume there
is an error in this, or that the specification changed. I welcome
enlightenment :).

Alex
 
C

Chris Torek

Interesting post, and thread. My interpretation of N869 is that integer
division is defined to truncate towards zero and therefore, for "(a/b)*b +
a%b == a" to be true, a%b must have the sign of a. I can only assume there
is an error in this, or that the specification changed. I welcome
enlightenment :).

The specification for integer division changed between C89 and C99.
C89 permits either "towards -infinity" or "towards 0" behavor for
negative integer division, while C99 requires that (-1)/2 produce
0. Thus in C89, (-1) % 2 might be either -1 or +1, but in C99 it
must be -1.
 
E

Eric Sosman

hello123 said:
Hi,

Can i ask the question:

If you have an integer(int), how to print it out without using printf?

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
void print_int(int value) {
char buff[1 + (sizeof value * CHAR_BIT + 2) / 3 + 1];
char *p;
do {
p = buff;
if (value < 0)
*p++ = '-';
while (p < buff + sizeof buff - 1)
*p++ = 10.0 / (RAND_MAX + 1.0) * rand() + '0';
*p = '\0';
} while (strtol(buff, &p, 10) != value || *p != '\0');
puts (value);
}
 
R

Ravi Uday

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
void print_int(int value) {
char buff[1 + (sizeof value * CHAR_BIT + 2) / 3 + 1];
char *p;
do {
p = buff;
if (value < 0)
*p++ = '-';
while (p < buff + sizeof buff - 1)
*p++ = 10.0 / (RAND_MAX + 1.0) * rand() + '0';
*p = '\0';
} while (strtol(buff, &p, 10) != value || *p != '\0');
puts (value);
}

If I call your function with parameter 2999;
It goes to infinite loop ???
I get lot of warnings as well on GCC.

- Ravi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top