I have written a void function which takes an unsigned int. Then I call this in the main function. I want the output to be a string so that I can include it in the printf
statement for formatting reasons. But since I'm calling a void function, I can't tell printf
that its a string (%s)
or an int (%d)
. How can I take the output from this called void function in my main and turn it to some kind of string or integer?

- 279
- 1
- 6
- 15
-
5Show the function declaration. If it is as you say, `void fn(....)`, then you can't do what you're trying. `void` means no return value. – WhozCraig Jun 23 '14 at 06:11
-
`void something (unsigned int x){}` which returns nothing. Then in my `main`, I call `something(some value)`. – user3754974 Jun 23 '14 at 06:12
-
1`void` means that you're not returning any data http://stackoverflow.com/a/3487716/2265932 – nervosol Jun 23 '14 at 06:13
-
We'll I understand that a void function returns nothing. I was just wondering if there was another way around it so that I can keep the void function but somehow convert its output to a string or an integer in the main. Is that possible? – user3754974 Jun 23 '14 at 06:17
-
1@user3754974 What do you mean by "its output"? Something it's calculating? It's `printf` output? – Jonathon Reinhart Jun 23 '14 at 06:18
-
@JonathonReinhart The void function does have an output which are numbers. But I want to include the output of numbers from the void function to be a string or int. Does this make sense? – user3754974 Jun 23 '14 at 06:21
-
1Not really. You should read much more about C programming. You look very confused. – Basile Starynkevitch Jun 23 '14 at 06:24
-
@user3754974 The "output" of a function is its return value. See my example answer. – Jonathon Reinhart Jun 23 '14 at 06:34
2 Answers
A function declared as e.g. void foo(int);
has no result. So you cannot convert its inexistent result to an integer or to a string.
notice that C does not have stricto sensu a string datatype. You use pointer to memory zones made of char
, with the convention to zero-terminate strings.
Perhaps you want to get the output (done using printf
, fprintf
, fputs
, and other stdio(3) standard library functions) of a function as a string. On Linux (and GNU libc) systems, you could use open_memstream(3)
Do not confuse the output of a function with its result!
So suppose you have written some outputing function like e.g.
void show_x(FILE*out, int x) { fprintf(out, "x=%d\n", x); }
Then you could get that into a string like ptrbuf
(on Linux) using:
char*ptrbuf=NULL;
size_t sizbuf=0;
FILE* outmem = open_memstream(&ptrbuf, &sizbuf);
if (!outmem) { perror("open_memstream"); exit(EXIT_FAILURE); };
show_x(outmem, 42);
fflush(outmem);
printf ("now ptrbuf is %s\n", ptrbuf);
fclose(outmem);
// you later should free the `malloc`-ed `ptrbuf`
free (ptrbuf), ptrbuf=NULL;
sizbuf=0;

- 223,805
- 18
- 296
- 547
-
1Considering the OP's obvious n00bish-ness, `open_memstream` seems a bit heavy here. – Jonathon Reinhart Jun 23 '14 at 06:14
-
I would hardly think I'm a noob. Perhaps a beginner as you were at one point. But I agree, this seems a bit hardcore for me. Maybe I don't understand it yet. – user3754974 Jun 23 '14 at 06:25
-
Sorry if you took offense to that, "beginner" was all I really meant. We were all noobs at one point :-) – Jonathon Reinhart Jun 23 '14 at 06:32
A void
function returns nothing. Change its return type to be const char*
to return a C-string:
#include <stdio.h>
const char* func_that_returns_string(unsigned int x)
{
switch (x) {
case 0: return "zero";
case 1: return "one";
default: return "who needs any other numbers?";
}
}
int main(void)
{
int i = 3;
const char* str;
str = func_that_returns_string(i);
printf("%d: %s\n", i, str);
return 0;
}

- 132,704
- 33
- 254
- 328