Absolutely — you're clearly not a beginner, so I’ll skip the basics and focus on the sharp edges and gotchas that distinguish C from C++ in practice. Here’s your razor-sharp C refresher, tailored to someone who knows C++ like the back of their hand:
🔥 1. struct is not a type name by default
struct Point { int x, y; };
// ❌ This won't compile:
Point p; // "unknown type name"
// ✅ You must use:
struct Point p;
// ✅ Or typedef it:
typedef struct Point { int x, y; } Point;
Point p; // Now this works
💡 Note: struct Point and Point live in separate namespaces. You can have both struct Point and typedef Point — they’re different things.
🔥 2. enum values pollute the enclosing scope (no namespace!)
enum Color { RED, GREEN, BLUE };
enum Size { SMALL, MEDIUM, LARGE };
// ❌ Conflicts!
Color c = SMALL; // Uh-oh: SMALL is from Size enum!
// ✅ Workaround: prefix or use typedef + scoped enums (C99+)
typedef enum { RED, GREEN, BLUE } Color;
typedef enum { SMALL, MEDIUM, LARGE } Size;
Color c = RED;
Size s = SMALL; // OK now
💡 Tip: Always use typedef enum for clean API design. In C99+, you can use enum Color : int (but still no scoping).
🔥 3. Booleans? Not a built-in type until C99
// Before C99: no bool
int flag = 1; // true
flag = 0; // false
// C99+: <stdbool.h>
#include <stdbool.h>
bool ok = true; // yes, 'true' and 'false' are keywords
bool result = (a == b);
💡 Without <stdbool.h>, true/false are undefined. Use 1/0 instead. Don’t rely on compiler extensions.
🔥 4. No function overloading → No void f(int) and void f(double)
// ❌ This is invalid in C:
void f(int x) { ... }
void f(double x) { ... } // error: redefinition
// ✅ Only one function with that name:
void f(int x) { ... }
void f(double x) { ... } // NO! Compile error.
💡 If you need multiple versions, suffix them: f_int(), f_double().
🔥 5. Arrays decay to pointers — but only when passed
void func(int arr[10]) {
// Inside: arr is actually a pointer to int (i.e., int*)
// The size 10 is ignored!
}
void func2(int *arr) {
// Same thing — no difference!
}
💡 But: sizeof(arr) inside func gives sizeof(int*), not 10 * sizeof(int).
⚠️ Local arrays don’t decay:
int a[10];
printf("%zu\n", sizeof(a)); // 40 (if int=4), NOT sizeof(int*)
🔥 6. No constructors/destructors
struct Data {
int x;
char *name;
};
// ❌ No automatic initialization
struct Data d; // uninitialized garbage!
// ✅ Must initialize manually:
struct Data d = { 42, "hello" };
// ✅ Or write init functions:
void data_init(struct Data *d, int x, const char *name) {
d->x = x;
d->name = strdup(name); // if needed
}
💡 Use memset(d, 0, sizeof(*d)) for zero-initialization.
🔥 7. static has different meaning than in C++
In C:
static at file scope → internal linkage (like static in C++ class scope).
static inside function → persists across calls, initialized once.
static int counter = 0; // File-scope: visible only in this translation unit
void increment() {
static int local_counter = 0; // Persists between calls
local_counter++;
}
🔄 In C++, static in a function means same thing — but static in class means something else. So context matters.
🔥 8. const is not a type modifier in the same way
const int *p; // pointer to const int → cannot modify *p
int * const p; // const pointer to int → cannot change p
const int * const p; // const pointer to const int
💡 Unlike C++, const doesn’t affect overload resolution. Also, const variables are not implicitly inline — they must be defined in exactly one TU.
⚠️ const int x = 5; in header? Risk of ODR violation unless declared static or inline (C99+):
// ✅ Safe in header:
static const int x = 5; // Each TU gets its own copy
// ✅ C99+:
inline const int x = 5; // One definition, usable in multiple TUs
🔥 9. _Bool, bool, true, false only in C99+
// C99 and later:
#include <stdbool.h>
bool flag = true;
_Bool b = 1;
💡 Pre-C99: use int for booleans (0 = false, non-zero = true). No standard bool.
🔥 10. Atomsics? Added in C11 (not C99!)
#include <stdatomic.h>
atomic_int counter = 0;
atomic_fetch_add(&counter, 1); // thread-safe increment
💡 Before C11: no atomic types. You had to rely on compiler intrinsics or external libraries.
🔥 11. No RAII / no destructors
FILE *fp = fopen("file.txt", "r");
if (!fp) return -1;
// ... do stuff ...
fclose(fp); // YOU MUST DO THIS
💡 No try/catch, no scope guard. Use goto cleanup patterns for error handling.
FILE *fp = fopen("file.txt", "r");
if (!fp) goto fail;
// ... use fp ...
fclose(fp);
return 0;
fail:
// cleanup code here
return -1;
🔥 12. #include vs #include_next
#include "header.h" — searches current directory first.
#include_next — used in headers to include next version in search path (e.g., for macro overrides).
💡 Rare, but useful in toolchain headers.
🔥 13. Variable-length arrays (VLAs) — C99 feature
void func(int n) {
int arr[n]; // OK in C99+, but not in C++!
// Stack allocation based on runtime value
}
⚠️ Not allowed in C++, and may cause stack overflow.
💡 Use malloc/calloc for large dynamic arrays.
🔥 14. No templates → no generic programming
// ❌ Can't do:
template<typename T> void swap(T *a, T *b) { ... }
// ✅ Use macros (dangerous):
#define SWAP(a, b, T) do { T tmp = a; a = b; b = tmp; } while(0)
// Or use void* (unsafe):
void swap(void *a, void *b, size_t size) {
char tmp[size];
memcpy(tmp, a, size);
memcpy(a, b, size);
memcpy(b, tmp, size);
}
💡 Macros are common, but prone to side effects. Use do { ... } while(0) to avoid issues.
🔥 15. Linkage rules: extern is mandatory for cross-TU declarations
// file1.c
int global_var = 42;
// file2.c
extern int global_var; // declare it
💡 If you don’t extern, you get a new variable in each TU — ODR violation.
⚠️ static variables are not extern — they’re private to the TU.
Summary: Key Differences from C++
| Feature | C | C++ |
|---|
struct name | Requires struct keyword | Can use as type directly |
enum values | Pollute global scope | Scoped via enum class |
bool | bool added in C99, stdbool.h | Built-in |
const | Not inline by default | inline variables in C++17+ |
static | Internal linkage or persistent local | Class member or persistent local |
RAII | No | Yes |
templates | No | Yes |
exceptions | No | Yes |
overloading | No | Yes |
new/delete | No | Yes |
Final Pro Tips
- Always
#include <stdbool.h> if using bool.
- Use
typedef enum for cleaner APIs.
- Avoid VLAs unless you know the bounds.
- Prefer
inline functions (C99+) over macros when possible.
- Use
static for internal helpers.
- Use
const everywhere you can — it’s part of the contract.
You’ve got this. You’re not rusty — just out of practice with the little things that make C… well, C. It’s all about discipline, manual memory management, and remembering that struct isn’t a type name unless you say so.
Now go break some things safely. 😎