• C

C Programming: Working with raw IO

How do I print int values using write(2)?

Seems like I can print strings using:

char *a = "abc";
write(STDOUT_FILENO, a, strlen(a));


printf("%s", a);

what about integers?
How would I use write(2) to do something like: printf("%d", myInt)
Who is Participating?
Julian HansenConnect With a Mentor Commented:
It depends how you want to write them - as binary ints or as printable numbers (strings)

To dump a binary representation of the int you could do something like this

int a = 2;
write(STDOUT_FILENO, &a, sizeof(int));

Open in new window

If you want to dump it as a string then sprintf it to a string first and then write it.
ozoConnect With a Mentor Commented:
char a[1000];
sprintf(a,"%d", myInt) ;
write(STDOUT_FILENO, a, strlen(a));
Julian HansenCommented:
char a[1000]; ? :)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.