Skip to content
Tutorials Center
My learning

Topics

C Reference ยท Functions and Scope

Return values in C

Reference for C return values: returning structs and pointers, never returning the address of a local, error codes, out-parameters and static buffers.

A C function returns at most one value, using return expression;. The value is converted to the declared return type, as if assigned. A function declared void returns nothing. Scalars, structs and unions can all be returned by value; arrays and functions cannot, though you can return pointers to them.

Returning a struct is a clean way to return several related values at once. Returning a pointer is how functions hand back allocated memory or a view into existing data, and it is the place where the most serious mistake happens: returning the address of a local variable.

C
struct point { int x, y; };
struct point make_point(int x, int y) {
    return (struct point){x, y};
}

Try it

C Returning structs and status codes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C Safe pointer returns

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
return-type name(parameters) {
    return expression;   /* converted to return-type */
}
void name(void) {
    return;              /* no value */
}
Name Description
return expr; Ends the function and delivers expr, converted to the return type.
return; Only in void functions.
falling off the end Allowed for void and for main (which then returns 0). For any other function, using the missing value is undefined.

Errors / undefined behavior: returning the address of a local variable or parameter produces a dangling pointer, and dereferencing it is undefined. Using the result of a non-void function that ended without a return is undefined. Returning an expression in a void function is a constraint violation.

Usage notes

  • Never return a pointer to automatic storage. int *f(void) { int x = 1; return &x; } compiles with a warning and breaks. Return allocated memory, a static object, or a pointer into data the caller owns.
  • Document ownership. If a function returns malloc memory, say that the caller must call free. If it returns a pointer into its argument, say how long it stays valid.
  • Static buffers are simple but not thread-safe and are overwritten by the next call (asctime, strtok have this trap). Prefer a caller-provided buffer: size_t format(char *out, size_t cap, ...).
  • Structs are returned by value. Small ones travel in registers; large ones may use hidden memory, but the language treats them the same. Returning a struct holding arrays copies the arrays.
  • Error reporting. Common styles are a status code with out-parameters, a sentinel (NULL, -1, EOF), or a struct with an ok field. Pick one convention per library and document it. Do not ignore return values of functions like fclose or scanf.
  • Conversions. return 3.9; from an int function returns 3. Return types are not checked for narrowing beyond optional warnings (-Wconversion).
  • [[nodiscard]] (C23) makes the compiler warn when callers ignore the result.
  • _Noreturn / [[noreturn]] marks functions like exit that never return.
  • Main returns int; return 0; and exit(0) mean success. See main().