I don't know what your string is, but I'm going to assume that it manages its own memory.

You have two solutions:

1: Return a struct which contains all the types you need.

struct Tuple {
    int a;
    string b;
};

struct Tuple getPair() {
    Tuple r = { 1, getString() };
    return r;
}

void foo() {
    struct Tuple t = getPair();
}

2: Use pointers to pass out values.

void getPair(int* a, string* b) {
    // Check that these are not pointing to NULL
    assert(a);
    assert(b);
    *a = 1;
    *b = getString();
}

void foo() {
    int a, b;
    getPair(&a, &b);
}

Which one you choose to use depends largely on personal preference as to whatever semantics you like more.

Answer from Travis Gockel on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ how-can-i-return-multiple-values-from-a-function
How can I return multiple values from a function? - GeeksforGeeks
July 23, 2025 - In C programming, a function can return only one value directly. However, C also provides several indirect methods in to return multiple values from a function.
People also ask

Can I use arrays to return multiple values from a function in C?
Yes, you can use arrays to return multiple values from a function in C by returning a pointer to an array. However, be cautious of the array's scope and lifetime; if it's a local array, use dynamic memory allocation or static storage to avoid returning a pointer to deallocated memory.
๐ŸŒ
vaia.com
vaia.com โ€บ how to return multiple values from a function in c
How to return multiple values from a function in C - Vaia
How can I return multiple values from a function in C using pointers?
To return multiple values from a function in C using pointers, you can pass pointers or references to variables as function parameters. By modifying the values at these addresses within the function, you effectively return multiple values to the calling function. Ensure the pointers are initialized before passing them.
๐ŸŒ
vaia.com
vaia.com โ€บ how to return multiple values from a function in c
How to return multiple values from a function in C - Vaia
What are the best practices for returning multiple values from a function in C?
Use structures to group related values, pass pointers to store output in function parameters, or return a dynamically allocated array. For better readability and memory management, consider structures for fixed types, and be cautious with memory allocation if using arrays.
๐ŸŒ
vaia.com
vaia.com โ€บ how to return multiple values from a function in c
How to return multiple values from a function in C - Vaia
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ how-to-return-multiple-values-from-a-function-in-c-or-cpp
How to return multiple values from a function in C or C++? - GeeksforGeeks
July 11, 2025 - New programmers are usually in the search of ways to return multiple values from a function. Unfortunately, C and C++ do not allow this directly. But fortunately, with a little bit of clever programming, we can easily achieve this.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-can-we-return-multiple-values-from-a-function-in-c-cplusplus
How can we return multiple values from a function in C/C++?
In C or C++, we cannot return multiple values from a function directly. In this Article, we will see how to use some trick to return more than one value from a function. Returning Multiple Values from a Function We can return multiple values from a f
๐ŸŒ
Quora
quora.com โ€บ What-are-all-possible-ways-of-returning-more-than-one-value-in-a-function-in-C
What are all possible ways of returning more than one value in a function in C? - Quora
Answer (1 of 7): Modern languages (C was around before type theory or ignored what there was in Pascal) make use of types. We have the base types built into languages char, integer, real, and donโ€™t forget boolean (because C did with resulting mess) with some basic compound types that map ...
Find elsewhere
๐ŸŒ
Vaia
vaia.com โ€บ how to return multiple values from a function in c
How to return multiple values from a function in C - Vaia
In C, you can return multiple values from a function using pointers, where you pass the addresses of variables to store the returned values, or using structures by defining a struct that contains the variables you want to return.
๐ŸŒ
YouTube
youtube.com โ€บ watch
Returning Multiple Values from a Function in C | C Programming Tutorial for Beginners - YouTube
Master C Programming and take your career to new heights with this clear, concise C course. Click here to Register: https://courses.sandipbhattacharya.com/co...
Published ย  January 12, 2023
๐ŸŒ
Reddit
reddit.com โ€บ r/cplusplus โ€บ [deleted by user]
How do I get my function to return multiple values?
October 1, 2022 - A function can return one result. You can make a struct, or a class, or an array to contain these multiple results, though. You could also make the arrays first and pass them to your function. You have a variety of options.
๐ŸŒ
Quora
quora.com โ€บ Can-I-have-multiple-return-values-in-C-1
Can I have multiple return values in C? - Quora
By the technical, literal interpretation of the question, the answer is no. C functions have exactly one return type (which might be [code ]void[/code]), and [code ]return[/code] at most one value. By a practical, pragmatic interpretation...
๐ŸŒ
YouTube
youtube.com โ€บ watch
How To "Return" More Than One Value From A Function | C Programming Example - YouTube
Example of how to "return" more than one value from a function in C, using pass-by-reference (i.e. pass-by-pointer). Source code: https://github.com/portfol...
Published ย  October 28, 2021
๐ŸŒ
Quora
quora.com โ€บ Is-it-possible-to-return-multiple-values-from-a-function-without-using-global-variables-in-C
Is it possible to return multiple values from a function without using global variables in C++? - Quora
Answer (1 of 7): As far as I know, no, You can not return multiple values from a function.. BUT, if You want to return multiple values of the same type, You can use std::vector or some alternatives like std::deque (if You wish queue like structure), ...
๐ŸŒ
Codeforwin
codeforwin.org โ€บ home โ€บ how to return multiple value from function in c programming?
How to return multiple value from function in C programming? - Codeforwin
July 20, 2025 - In C you can pass parameters to a function either by value or by reference. Changes made to variables passed by reference persists after the function. Hence you can pass any number of parameters as reference, which you want to return from function.
๐ŸŒ
Arduino Forum
forum.arduino.cc โ€บ projects โ€บ programming
returning two values from a function - Programming - Arduino Forum
I'm breaking up some messy code into separate functions, and can't seem to figure out how to pass two parameters into a function, and get two back. The passing part is easy - I can just call get_angles(somenumber, somenuโ€ฆ
Published ย  February 14, 2012
๐ŸŒ
Quora
quora.com โ€บ Can-we-return-more-than-one-value-using-function-in-C-language-at-a-time
Can we return more than one value using function in C language at a time? - Quora
Answer (1 of 10): We all know that a function in C can return only one value. So how do we achieve the purpose of returning multiple values. Well, first take a look at the declaration of a function. int foo(int arg1, int arg2); So we can notice ...
Top answer
1 of 4
6

Wow. This is a spectacularly misleading result.

No, you can not return multiple values like this in C. A function can have at most one single return value. You can't use a comma to return multiple values like this.

But, if you can't do it, then why did it seem to almost work? That's a rather complicated question, that it probably won't be possible to completely answer here.

Your program has two, completely unrelated problems. The first problem, which isn't too serious, is the line

return a, b, c;

at the end of function sum. In English, this line basically says, "Take the value a and throw it away, then take the value b and throw it away, then take the value c and have it be the actual return value." (See What does the comma operator do? for more about this comma operator.)

And then the second, much more serious problem is the line

printf("%d %d %d", x);

in main. Your compiler should have warned you about this problem; mine says warning: more '%' conversions than data arguments. You're telling printf, "I'm going to give you three int values to print". But you lied, because you're then giving it just one int value, x. So printf is blindly reaching for the other two values you promised, and getting... what?

By a reasonably incredible coincidence, when printf reaches out to fetch the values you didn't pass, it is somehow grabbing... one of the values that got thrown away by the poorly-written return statement back in function sum. I'm not even sure how this happened, although when I tried your program on my computer, I got a similar result.

This may be too obvious to mention, but: this result is not guaranteed, it's not something to depend on, and it's not even something to learn from. If you want to return multiple values from a function, there are other ways to do that, as this question's other answers have suggested.

2 of 4
4

Short answer: no, you cannot return multiple values (at least, not as you did). Good news, you do have some ways to return multiple values from a function:


Method 1: use a struct

typedef struct result {
  int a;
  int b;
  int c;
} result_t;

result_t foo(int a, int b, int c) {
  return (result_t) { a + 1, b + 1, c + 1 };
}

int main(void) {
  result_t res = foo(1, 2, 3);
  printf("res.a is %d\n", res.a); // OUTPUT: res.a is 2
  return 0;
}

Method 2: use pointers as function arguments

void some_calc(int input, int* output1, int* output2) {
  *output1 = input * 2;
  *output2 = input / 2;
}

int main(void) {
  int res1, res2;
  some_calc(50, &res1, &res2);
  printf("50 * 2 = %d and 50 / 2 = %d\n", res1, res2);
  return 0;
}
๐ŸŒ
Quora
quora.com โ€บ Can-we-return-multiple-values-from-a-C-C-function
Can we return multiple values from a C/C++ function? - Quora
Answer (1 of 2): There is no such thing as C/C++! However, in both C and C++ it is possible to return any number of values from a function, even if the return of different types is requiredโ€ฆ.