
C: examples explained
Maturity and boundaries: bounded
Implemented scope: Reference frontend for native Windows x64 EXE/DLLs with documented C89–C2y modes, preprocessing, Windows ABI and selected GCC/MSVC extensions.
Boundary: Full standards conformance is not claimed; long double is 64-bit, _BitInt reaches 128 bits, and some language, assembler and linker forms are unsupported.
C supported language forms and boundary
Browse the language-specific supported-command and scope reference, with searchable categories and explicit boundaries.
Open scope referenceThe C input uses main and C expressions. A return value can be checked as the process exit code; printf produces output instead.
Twelve short examples and one larger program. Choose a topic or work through the examples in order.
1. 1 + 2 + 3 + 4 + 5 = 15
The numbers 1 through 5 are added to 15. Return value 15 is checked as the process exit code.
#include <stdio.h>
int main(void) {
int sum = 0;
for (int i = 1; i <= 5; i++) {
sum += i;
}
return sum; /* 15 */
}Expected exit code: 15
PBXB64.exe sum15-c.c -O2 -o example.exe .\example.exe $LASTEXITCODE
Executed natively with the V22 candidate from 2026-09-30; final-compiler verification pending.
2. The smallest result contract
main returns 42 without printing anything. In PowerShell, $LASTEXITCODE reads the most recently launched process result.
int main(void) { return 42; }
Expected exit code: 42
PBXB64.exe simple.c -O2 -o example.exe .\example.exe $LASTEXITCODE
Canonical test case with an expected result; no new native V22 check here. Final execution pending.
3. A hexadecimal mask with &
0xFF & 0x0F retains only the lower four bits, producing 15. The conditional expression returns 0 when correct and 1 otherwise: an internal program check.
int main(void)
{
return (0xFF & 0x0F) == 15 ? 0 : 1;
}
Expected exit code: 0
PBXB64.exe test_hex_integer_bitwise.c -O2 -o example.exe .\example.exe $LASTEXITCODE
Canonical test case with an expected result; no new native V22 check here. Final execution pending.
4. Several kinds of control flow
Step-by-step flow
- The first loop sums 0 through 9 to 45; while counts back to 0 and do-while increases to 5.
- switch sets sum to 10. The next loop skips 2 and adds 0 + 1 + 3 + 4 to reach 18.
- goto reaches done. The fixed return value 42 checks completion rather than every intermediate value separately.
int main() {
int i, sum = 0;
/* for loop */
for (i = 0; i < 10; i++) sum += i;
/* while loop */
while (sum > 0) sum--;
/* do-while loop */
do { sum++; } while (sum < 5);
/* switch with break and default */
switch (sum) {
case 5: sum = 10; break;
default: sum = 0;
}
/* continue */
for (i = 0; i < 5; i++) {
if (i == 2) continue;
sum += i;
}
/* goto */
if (sum == 18) goto done;
sum = 99;
done:
return 42;
}
Expected exit code: 42
PBXB64.exe c-test_c_stmts.c -O2 -o example.exe .\example.exe $LASTEXITCODE
Expected-result contract from tests/validate.toml. This website expansion adds no new native compiler verification. Execution with the qualified final compiler remains pending.
5. Initialized and zero-initialized globals
Step-by-step flow
- global_a and global_b are defined with 10 and 20.
- global_c has static storage duration and is zero-initialized without an explicit initializer.
- The conditional expression returns 0 when the sum is 30, otherwise 1.
/* PBXB64: C Global Variable Test */
int global_a = 10;
int global_b = 20;
int global_c;
int main(void)
{
int sum = global_a + global_b + global_c;
return (sum == 30) ? 0 : 1;
}
Expected exit code: 0
PBXB64.exe c-test_globals.c -O2 -o example.exe .\example.exe $LASTEXITCODE
Expected-result contract from tests/validate.toml. This website expansion adds no new native compiler verification. Execution with the qualified final compiler remains pending.
6. Pointer differences inside one array
Step-by-step flow
- last points to the fourth element of values.
- last - values is 3 in units of int elements rather than bytes.
- The reverse difference is -3. Both pointers belong to the same array.
int main(void)
{
int values[4] = { 10, 20, 30, 40 };
int *last = &values[3];
if (last - values != 3) return 1;
if (values - last != -3) return 10 + (values - last);
return 0;
}
Expected exit code: 0
PBXB64.exe c-test_c_pointer_difference_array.c -O2 -o example.exe .\example.exe $LASTEXITCODE
Expected-result contract from tests/validate.toml. This website expansion adds no new native compiler verification. Execution with the qualified final compiler remains pending.
7. The address of a compound literal
Step-by-step flow
- (uint8_t){0} creates an initialized object within main.
- & takes its address and passes it to takes_ptr.
- The test checks a non-null pointer; its lifetime covers the immediate function call.
typedef unsigned char uint8_t;
int takes_ptr(uint8_t* value)
{
return value != 0;
}
int main(void)
{
return takes_ptr(&(uint8_t){0}) ? 0 : 1;
}
Expected exit code: 0
PBXB64.exe c-test_compound_literal_address.c -O2 -o example.exe .\example.exe $LASTEXITCODE
Expected-result contract from tests/validate.toml. This website expansion adds no new native compiler verification. Execution with the qualified final compiler remains pending.
8. Return a struct by value
Step-by-step flow
- Pair contains two long long fields.
- (Pair){0} initializes both fields to 0 and is returned as a struct.
- main reads field a and converts it to int for the process result.
typedef struct Pair {
long long a;
long long b;
} Pair;
Pair make_pair(void)
{
return (Pair){0};
}
int main(void)
{
return (int)make_pair().a;
}
Expected exit code: 0
PBXB64.exe c-test_compound_literal.c -O2 -o example.exe .\example.exe $LASTEXITCODE
Expected-result contract from tests/validate.toml. This website expansion adds no new native compiler verification. Execution with the qualified final compiler remains pending.
9. File-local data and function
Step-by-step flow
- static_val is initialized to 42 and restricted to this translation unit.
- The static function add_static adds its argument to that value.
- add_static(10) must return 52; main checks that result.
static int static_val = 42;
static int add_static(int x)
{
return x + static_val;
}
int main(void)
{
if (add_static(10) != 52) return 1;
return 0;
}
Expected exit code: 0
PBXB64.exe c-test_static_extern.c -O2 -o example.exe .\example.exe $LASTEXITCODE
Expected-result contract from tests/validate.toml. This website expansion adds no new native compiler verification. Execution with the qualified final compiler remains pending.
10. Floating values as logical conditions
Step-by-step flow
- d contains 0.0 and is false in a logical condition.
- d || 1 is true, while d && 1 is false.
- The two if statements return early only if an expected result is wrong.
int main(void)
{
double d = 0.0;
if (!(d || 1)) return 1; /* 0.0 || 1 must be true */
if (d && 1) return 2; /* 0.0 && 1 must be false */
return 0;
}
Expected exit code: 0
PBXB64.exe c-test_float_logical.c -O2 -o example.exe .\example.exe $LASTEXITCODE
Expected-result contract from tests/validate.toml. This website expansion adds no new native compiler verification. Execution with the qualified final compiler remains pending.
11. Assign a function pointer
Step-by-step flow
- Callback names a pointer type for a function taking and returning int.
- callback_impl is assigned to that pointer without writing &.
- The test checks cb != 0; it does not invoke the function.
typedef int (*Callback)(int value);
static int callback_impl(int value)
{
return value + 1;
}
int main(void)
{
Callback cb = callback_impl;
return cb != 0 ? 0 : 1;
}
Expected exit code: 0
PBXB64.exe c-test_function_designator_pointer.c -O2 -o example.exe .\example.exe $LASTEXITCODE
Expected-result contract from tests/validate.toml. This website expansion adds no new native compiler verification. Execution with the qualified final compiler remains pending.
12. Floating literals without a leading zero
Step-by-step flow
- The spelling .5 means 0.5 with type double.
- The f suffix in .25f selects float.
- Both values are checked against their corresponding spellings with a leading zero.
int main(void)
{
double x = .5;
float y = .25f;
if (x != 0.5) return 1;
if (y != 0.25f) return 2;
return 0;
}
Expected exit code: 0
PBXB64.exe c-test_c_leading_dot_float.c -O2 -o example.exe .\example.exe $LASTEXITCODE
Expected-result contract from tests/validate.toml. This website expansion adds no new native compiler verification. Execution with the qualified final compiler remains pending.
Larger example: Calculate and check complex values
Step-by-step flow
- __builtin_complex builds values from real and imaginary parts; dc is 1 + 2i.
- Addition produces 3 + 5i; multiplication by 2 + 0i produces 2 + 4i.
- creal and cimag read individual parts. Error codes 1 through 5 identify checks; successful completion returns 0.
/* PBXB64 C frontend: exercise complex-number value lowering
* (c_frontend_lexer_value.c: c_value_to_ir, c_complex_parts_from_value,
* c_emit_complex_temp, c_emit_complex_binary, c_emit_complex_extract_part).
*/
int main(void)
{
float _Complex fc = __builtin_complex(3.0f, 4.0f);
double _Complex dc = __builtin_complex(1.0, 2.0);
/* complex addition and multiplication */
double _Complex sum = dc + __builtin_complex(2.0, 3.0);
double _Complex prod = dc * __builtin_complex(2.0, 0.0);
double r = creal(sum);
double i = cimag(sum);
if (r != 3.0) return 1;
if (i != 5.0) return 2;
if (creal(prod) != 2.0) return 3;
if (cimag(prod) != 4.0) return 4;
/* scalar-to-complex conversion through c_complex_parts_from_value */
double _Complex from_real = __builtin_complex(7.0, 0.0);
if (creal(from_real) != 7.0) return 5;
(void)fc;
return 0;
}
Expected exit code: 0
PBXB64.exe c-test_c_complex.c -O2 -o example.exe .\example.exe $LASTEXITCODE
Expected-result contract from tests/validate.toml. This website expansion adds no new native compiler verification. Execution with the qualified final compiler remains pending.
All sources for this tutorial
Each example has its own folder; the README lists compiler commands. Included support files retain matching paths.
Download all 13 examples as ZIPBridging
A C function with int64_t parameters can be called by the B64 example. Its symbol name and types must match the extern contract.
Explore complete source pairs