Skip to content
TheChatArchiveThe Chat Archive
gpt-5-high· OpenAIHugging Face Arena Expert 5K

I write C++ for a long time. I do work with C sometimes, but it's really far in between.…

  1. User

    I write C++ for a long time. I do work with C sometimes, but it's really far in between. So, give me a quick rundown to refresh my memory on the important C stuff. Keep in mind, I know C++ perfectly, so no need to explain "it has pointers" or "it's a low-level language". Also, I do write C, so I know it's not C++ (it doesn't have classes, duh). But there are all these small details that you tend to miss or forget: the struct keyword is mandatory, unless you typedef it to a new typename, struct namespace is different from typedef namespace, enum values are polluting the enclosing scope, atomics were only added in c99, booleans were not what you expect for a damn long time.... That kind of stuff.

  2. Assistant

    Here’s a C refresher for someone fluent in C++—focused on the small-but-sharp edges and “yeah, that’s different in C” details. I’ll assume C11/C17 unless noted.

    Standards quick map (what showed up when)

    • C89/90: the original ANSI C most old code targets.
    • C99: // comments, mixed declarations/statements, inline, VLAs, long long, stdint.h, stdbool.h/_Bool, designated inits, compound literals, complex, variadic macros. Implicit int and implicit function declarations removed.
    • C11: atomics (stdatomic.h/_Atomic), memory model, _Generic, _Static_assert, _Alignas/_Alignof, _Noreturn, _Thread_local, threads.h (optional).
    • C17: bug-fix release of C11 (no new features).
    • C23: lots of QoL updates, but not ubiquitous yet—stick to C11/C17 for portability.

    Namespaces, tags, typedefs, enums

    • struct/union/enum tags live in the “tag namespace,” separate from identifiers.
      • You must write struct S unless you create a typedef:
        • typedef struct S S; then you can use S without struct.
    • typedef names share the ordinary identifier namespace in C (unlike C++). You cannot reuse the same name for a typedef and a variable/function in the same scope:
      • typedef int Foo; int Foo; // error in C
    • Enumerators pollute the enclosing identifier namespace (no scope, no enum class):
      • enum E { A, B }; int A; // error
    • You can forward-declare struct/union tags, but not enums in standard C:
      • struct X; struct X* p; // OK
      • enum E; // not allowed in standard C (needs full definition)
    • Member names are in their own per-struct/union namespace (like C++).

    Types, literals, and small type gotchas

    • Plain char signedness is implementation-defined (same as C++).
    • Character literals have type int in C (not char). sizeof('a') == sizeof(int).
    • String literals have type array of char (not const); they are not modifiable. char* p = "hi"; is allowed but writing through p is UB. In practice, treat them as const and consider using -Wwrite-strings.
    • _Bool exists since C99; <stdbool.h> defines bool/true/false as macros mapping to _Bool. No keyword bool unless you include the header.
    • The usual arithmetic conversions: mixing signed/unsigned tends to promote to unsigned—same traps as C++.
    • Enum types are integer types capable of holding the enumerators (often int). No fixed underlying type.

    Functions, prototypes, and inline

    • int f(); in C means “no prototype” (i.e., unknown parameters), not “no parameters.” Use int f(void) for a function that takes nothing.
      • Enable -Wstrict-prototypes to catch this.
    • Implicit function declarations are not allowed since C99 (calling an undeclared function is an error).
    • Overloading doesn’t exist (yep). For overload-like behavior, use _Generic (C11).
    • inline semantics in C are not like C++:
      • In headers, prefer static inline for helpers:
        • static inline int sq(int x) { return x*x; } // one internal copy per TU
      • If you want a single externally linkable definition plus inline uses:
        • header: inline int f(int x) { … } extern int f(int);
        • one .c: int f(int x) { … }
      • Don’t just put non-static inline in headers without an extern or you’ll get missing/duplicate symbols depending on the compiler mode.

    Linkage, storage, and ODR-adjacent stuff

    • File-scope tentatives: int x; at file scope is a tentative definition; if no other definition appears in that TU, it becomes a real definition with 0 init. Do not put that in a header unless you mean it.
    • Unlike C++, const at file scope has external linkage by default in C. So this in a header:
      • const int N = 42; // multiple definition at link time
      • Fix: static const int N = 42; or extern const int N; (declare in header), const int N = 42; (define in one .c).
    • static gives internal linkage for functions/objects at file scope; extern declares external linkage.

    Initialization tricks unique to C (or different from C++)

    • Designated initializers (C99):
      • struct S s = { .y = 2, .x = 1 }; int a[5] = { [2] = 7 };
    • Compound literals (C99):
      • int* p = (int[]){1,2,3};
      • foo((struct S){.x=1,.y=2});
      • At block scope the object has automatic storage; at file scope it has static storage.
    • Flexible array members (C99, last member only):
      • struct V { size_t n; int a[]; }; // allocate with sizeof(struct V)+n*sizeof(int)
      • Not in standard C++.

    Arrays, pointers, and parameters

    • Arrays decay to pointers in most expressions; in parameter lists, int a[] is just int* a. Multi-dim arrays in parameters must name all but the first dimension:
      • void f(size_t n, int a[n][4]); // VLA-based prototype (C99)
    • Parameter qualifiers like restrict and [static N] are hints/contracts to the optimizer:
      • void g(int a[static 16]); // caller promises at least 16 elements
    • VLAs: C99 added them; C11 made them optional. Many compilers support; don’t use in freestanding or kernel-like code unless you’re sure.

    Memory and void*

    • void* implicitly converts to/from any object pointer in C. Don’t cast malloc:
      • int* p = malloc(n * sizeof *p); // in C, no cast; casting can hide missing <stdlib.h>.
    • realloc rules:
      • On failure returns NULL and leaves the original block untouched.
      • realloc(p, 0) is either free(p) and returns NULL, or returns a pointer that can be passed to free (implementation-defined). Don’t rely on the latter.
    • free(NULL) is a no-op.

    Atomics, threading, and qualifiers

    • Atomics are C11, not C99. Include <stdatomic.h> or use the _Atomic qualifier:
      • _Atomic int x; or atomic_int x; atomic_fetch_add(&x, 1);
    • Memory orders mirror C++ (memory_order_relaxed/acquire/release/acq_rel/seq_cst).
    • C11 threads (<threads.h>) exist but were historically spotty; check availability.
    • restrict (C99) is a promise about non-aliasing of the pointed-to region; hugely important for optimization. C++ has no standard restrict (only vendor extensions).
    • volatile is for hardware/async visibility, not for atomicity or inter-thread ordering.

    Preprocessor and poor-man’s generics

    • Variadic macros since C99: #define LOG(fmt, ...) …
    • _Generic (C11) to emulate overloads:
      • #define abs(x) _Generic((x), long: labs, long long: llabs, default: fabs)(x)
    • Many libc “functions” may be macros (e.g., isdigit). Beware of side effects in arguments.

    Library/format-specifier traps

    • Use the right printf/scanf specifiers; mismatch is UB:
      • size_t: %zu, ptrdiff_t: %td, int64_t: PRId64 from <inttypes.h>.
    • math.h functions are type-specific; tgmath.h (C99) provides type-generic macros.
    • errno is a macro; it may be thread-local.

    A few semantic/UB sharp edges worth re-remembering

    • Order of evaluation for function arguments and most operators is unspecified (still true in C17). Don’t write f(i++, i++) or similar.
    • Sequence/“unsequenced” bugs: i = i++; is undefined.
    • Integer division by zero is UB; division with negatives truncates toward zero since C99.
    • Shifting negative values is implementation-defined (right shift) or UB (some left-shifts). Left-shifting into/sign overflow: UB.
    • Strict aliasing: accessing an object via an incompatible pointer type is UB. Exceptions: char/unsigned char, and some union/standard exceptions. When in doubt, memcpy. Consider -fno-strict-aliasing if you must.
    • Union type punning: reading through a different member than the one most recently written is implementation-defined. Works on common compilers, but know what you’re doing (or memcpy).

    Little differences from C++ that bite

    • sizeof('a') is sizeof(int) in C; in C++ it’s sizeof(char).
    • String literals: char[] in C vs const char[] in C++. Writing to them is UB in both; C just types them differently.
    • No exceptions/destructors/RAII—obviously—but it means every path must free/close manually; cleanup labels + goto are idiomatic C.
    • No overloading; int f() vs int f(void) difference is a common source of bugs when sharing headers.
    • No templates; use macros/_Generic.
    • No operator new/delete; malloc/free only; placement new patterns don’t apply.
    • Bitfields: types allowed are implementation-defined beyond _Bool/signed/unsigned int; alignment/packing is implementation-defined. Don’t assume layout across compilers/ABIs.

    Pragmatic build switches that help

    • Compile with: -std=c11 -Wall -Wextra -Wpedantic -Wvla -Wshadow -Wconversion -Wstrict-prototypes -Wmissing-prototypes -Wwrite-strings -Werror=implicit-function-declaration
    • Sanitizers: -fsanitize=address,undefined,leak (where available).
    • Since GCC 10, -fno-common is default; don’t rely on “common” tentative definitions across TUs.

    Quick examples to jog memory

    • Typedef vs tag namespaces:
      • typedef struct S S; struct S { int x; }; S s; struct S t; // both OK
    • “No prototype” vs “no params”:
      • int f(); // no prototype: arguments not checked
      • int f(void); // truly takes no arguments
    • Designated init and compound literal:
      • struct P { int x, y; };
      • struct P p = { .y = 2, .x = 1 };
      • take_point(&(struct P){ .x = 3, .y = 4 });
    • Atomics (C11):
      • #include <stdatomic.h>
      • atomic_int c = 0;
      • int old = atomic_fetch_add_explicit(&c, 1, memory_order_relaxed);

    If you want, tell me which C standard you target and your typical platforms (glibc/msvcrt/newlib/uClibc, 32/64-bit, embedded vs hosted). I can tailor this to the subset you actually use and point out which features are safe to assume vs “nice if available.”

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.