Reputation: 58
I'm a noob on C and trying to use write()
function to show an integer.
These is my code:
int n = 7;
write(1, &n, 4);
I want to show 7
, but the program shows nothing or other strange character when I set n
to a big number.
What am I missing?
Upvotes: 3
Views: 35758
Reputation: 41
void ft_putnbr_fd(int nu, int fd)
{
long int n;
n = nu;
if (n < 0)
{
ft_putchar_fd('-', fd);
n = n * (-1);
}
if (n < 10)
{
ft_putchar_fd(n + '0', fd);
}
else
{
ft_putnbr_fd(n / 10, fd);
ft_putchar_fd(n % 10 + '0', fd);
}
}
Upvotes: 4
Reputation: 47952
When you say something like
int n = 7;
write(fd, &n, sizeof(int));
you are taking the individual bytes corresponding to the integer n
and writing them out to the file descriptor fd
.
And that works just fine, unless what you wanted was a human readable representation of the integer n
. That's the representation you'd get if you had instead written
printf("%d\n", n);
It's printf
's job, when you use a format specifier like %d
, to create a human-readable string of characters corresponding to a data object.
So if you want to print an int
in a human-readable way, printf
is definitely your best bet. If for some reason you can't use printf
, you can use sprintf
to create an in-memory string, then use write
to write that out:
char tmpbuf[30];
sprintf(tmpbuf, "%d", n);
write(fd, tmpbuf, strlen(tmpbuf));
If for some reason you can't use sprintf
, you might be able to use itoa
:
char tmpbuf[30];
itoa(n, tmpbuf, 10);
write(fd, tmpbuf, strlen(tmpbuf));
However, the itoa
function is not standard. If you don't have it or can't use it, your last resort would be to convert an integer to its string representation yourself, by hand. That's such a common question that I'm not going to provide Yet Another answer to it here, but see the linked question, or this one.
You didn't ask, but if there's ever the related problem of printing a floating point number using write
, there are some hints in the comments at this question.
Upvotes: 1
Reputation: 222679
Objects like int
are represented in memory with various bits. The write
routine transmits exactly those bits of memory to its destination.
Terminals are not designed to display arbitrary bits of memory. They do not interpret the bits to mean an int
or other object and then display that interpretation. Generally, we transmit characters to terminals. More specifically, we send codes that represent characters. Terminals are designed to receive these codes and display little pictures of writing (characters, glyphs, emoji, whatever).
To make a terminal display “7”, we need to send it the code for “7”. A common code system for characters is ASCII (American Standard Code for Information Interchange). The ASCII code for “7” is 55. So, if you do this:
char x = 55;
write(1, &x, 1);
then the terminal will draw “7” on its display, if ASCII is being used.
So write
is the wrong routine to use to display int
values for a human to read. Instead, you normally use printf
, like this:
printf("%d", n);
The f
in printf
stands for formatted
. It examines the bits that represent the value in n
and formats the represented value as characters intended for humans to read, and then it writes those characters to standard output.
If you want to use write
to transmit characters to the terminal, you can use sprintf
to get just the formatting part of printf
without the printing part. For starters, this code will work:
char buffer[80]; // Make space for sprintf to work in.
int LengthUsed = sprintf(buffer, "%d", n); // Format n in decimal.
write(1, buffer, LengthUsed); // Write the characters.
(More sophisticated code would adapt the buffer size to what is needed for the sprintf
.)
Upvotes: 10
Reputation: 48
Write function is a system call. It is used to write the content of a buffer to a declared output or to a stream. You should not use write.
Instead, you must be using printf(" ", ...)
. In your case:
printf("%d", n);
or
print("%d\n",n);
if you want to write it on a line and do an end line(jump the next).
For more information about printf see: printf
Upvotes: -1
Reputation: 15042
When you want to print it out on the command prompt only use printf()
instead:
int n = 7;
printf("%i",n);
Is there a special intention for using the write()
function?
If so, please add more of your code.
Upvotes: 0