Reputation: 7
I want to print the char, int, float and double values without using format specifiers in my c program. I can able to print the string using the below code:
char s[] = "Hello\n";
fprintf(stdout, s);
how can I print the other data type values?
Upvotes: 0
Views: 3961
Reputation: 154250
To print a char
as a character use fputc()
.
To print an integer (including char
) in its decimal form, call either print_unsigned()
or print_signed()
, depending on if it is a signed integer or an unsigned integer.
The below uses recursion to print the most significant digits first.
For signed integers, it flips positive numbers to negative avoiding the undefined behavior of -INT_MIN
.
int print_unsigned(uintmax_t x) {
if (x >= 10) {
if (print_unsigned(x / 10) == EOF) return EOF;
}
return fputc('0' + x % 10, stdout);
}
static int print_signed_helper(intmax_t x) {
if (x <= -10) {
if (print_signed_helper(x / 10) == EOF) return EOF;
}
return fputc('0' - x % 10, stdout);
}
int print_signed(intmax_t x) {
if (x < 0) {
if (fputc('-', stdout) == EOF) return EOF;
} else {
x = -x; // overflow not possible
}
return print_signed_helper(x);
}
The above stops early if the output causes an output error. EOF
is something rarely returned from fputc()
.
To printf a float
of double
: TBD code.
Upvotes: 0
Reputation: 223161
To print a char
, use:
fputc(c, stream)
(If the stream is stdout
, you can use putchar(c)
.)
To print an int
:
int
is negative, print “-”.x % 10
, to calculate the least significant digit of a number, and you can use division, such as x / 10
, to remove that digit.%
operator will return negative values. Some people attempt to deal with this by negating the integer if it is negative. However, if the number is the least possible int
, this may overflow. E.g., in many C implementations, the least int
value is -2,147,483,648, but it cannot be negated because the greatest int
is 2,147,483,647.'0'
, such as int d = x % 10; char c = d + '0';
. The C standard guarantees that this produces the appropriate character in c
.To print a float
or double
:
The above should suffice to get you started. It is not complete code, obviously.
Upvotes: 4
Reputation: 4206
The short answer is that while it may be possible to hack something together that will do something RESEMBLING what you want (along the lines of Peter Miehle's solution posted in another answer), fundamentally C is not designed for this kind of functionality, and there is no support for it in the language. What you want is function overloading, which C++ and many other higher-level languages provide.
Even Peter Miehle's solution cannot be implemented as a function (in C), because what kind of argument would the function take? Either it is passed a type, in which case we KNOW the type and may as well use printf
, or it is passed e.g. a void pointer, in which case, how can it implement the arithmetic operators without knowing the underlying data type the pointer points to?
Upvotes: -1
Reputation: 6070
just one thought, not very optimal:
int myvalue = 12345;
char buffer[100];
size_t index = 0;
while (myvalue) {
buffer[index] = '0' + myvalue % 10;
myvalue = myvalue / 10;
index++;
}
buffer[index] = '\0';
reverse(buffer);
fprintf(stdout, buffer);
you have to consider the negative sign. And the sizeof buffer (100 is a very bad guess).
Upvotes: 1