C passes everything by value. The function receives a copy of each argument, so changing a parameter does not change the caller's variable. Pointer arguments are also copied, but the copy still points to the original object.
Remember
void bump(int n) { n = n + 1; } /* changes the copy */
int x = 5;
bump(x); /* x is still 5 */
Reference
Cheat Sheet
Keep these facts handy while you work with Pass by Value in C.
Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Pass by Reference.
Example 1 — The caller's variable is unchanged
The function modifies only its own copy.
C
#include <stdio.h>
void add_one(int n) {
n = n + 1;
printf("inside: %d\n", n);
}
int main(void) {
int x = 5;
add_one(x);
printf("outside: %d\n", x);
return 0;
}
Output
inside: 6
outside: 5
How It Works
n starts as a copy of x (5).
Inside, n becomes 6.
When the function returns, n disappears and x is still 5.
Edge Cases & Pitfalls
Check these before relying on Pass by Value in real programs.
expect
Expecting a swap to work
A swap(int a, int b) function swaps only its copies. Use pointers.
struct
Copying big structs
Passing a large struct by value copies every byte. Pass const struct T * instead.
Remember
Key Takeaways
Always: C is pass by value.
Pointers: the copy still points to the original.
Performance: avoid copying big structs.
One line: C passes everything by value.
Frequently Asked Questions
Not as a language feature. You simulate it by passing a pointer, which is itself passed by value but lets the function change the pointed-to object.
Because it received a copy. Pass &variable and dereference the pointer parameter inside the function.
Continue with "C Pass by Reference" next. Pass addresses so a function can modify the caller's variables. The C Functions hub lists every topic in order.