Basic Workflow
A typical Z3 C program follows this pattern:- Create a configuration and context
- Declare variables and constraints
- Create a solver and add assertions
- Check satisfiability
- Extract results (models, proofs, etc.)
- Clean up resources
Hello Z3
Let’s start with a simple example:Always delete the config after creating the context, and delete the context when done to prevent memory leaks.
Configuration and Context
The context manages all Z3 objects. The configuration sets parameters before creating the context.Error Handling
Creating Variables and Expressions
Boolean Variables
Integer Variables
Integer Constants
Building Formulas
Logical Operations
Arithmetic Operations
Using Solvers
Creating and Using a Solver
Complete Example: De Morgan’s Law
This example proves De Morgan’s law:¬(x ∧ y) ⟺ (¬x ∨ ¬y)
Complete Example: Finding a Model
Find values for x and y such thatx + y > 5 and x - y < 2:
Memory Management
Reference Counting
AST Reference Counting
Most AST nodes are automatically managed by the context, but if you need to keep them across multiple solver calls:Printing and Debugging
Best Practices
- Always create a fresh context for each independent solving task
- Enable model generation in the config if you need to extract solutions
- Use reference counting properly to avoid memory leaks
- Check error codes or set up error handlers for production code
- Clean up resources in reverse order of creation
Next Steps
API Reference
Explore the complete C API
Examples
View more C examples on GitHub
