C doesn't include native support for graph drawing or manipulation, but there are several libraries that offer robust data structures, algorithms, and rendering support for graph-related work.
Graphviz provides automatic layout of nodes and edges, and its libgvc
and libcgraph
libraries allow C/C++ integration with the DOT language parser and renderer.
#include <graphviz/cgraph.h>
#include <graphviz/gvc.h>
Agraph_t *g = agopen("G", Agdirected, NULL);
Agnode_t *n = agnode(g, "Node1", 1);
Agnode_t *m = agnode(g, "Node2", 1);
agedge(g, n, m, NULL, 1);
GVC_t *gvc = gvContext();
gvLayout(gvc, g, "dot");
gvRenderFilename(gvc, g, "png", "graph.png");
gvFreeLayout(gvc, g);
agclose(g);
gvFreeContext(gvc);
A high-performance library for graph algorithms and analysis. It is widely used in network science and data analysis.
#include <igraph.h>
igraph_t g;
igraph_vector_t edges;
igraph_vector_init(&edges, 0);
VECTOR(edges)[0] = 0; VECTOR(edges)[1] = 1;
igraph_create(&g, &edges, 0, IGRAPH_UNDIRECTED);
igraph_destroy(&g);
igraph_vector_destroy(&edges);
Note: While not native to C, NetworkX can be called via embedded Python in C using CPython API or CFFI, useful for advanced graph analytics.
Lemon is a modern C++ library focused on graph and network optimization, but C wrappers can be written for use in C projects.
For in-memory graph representation, independent of rendering (i.e., building data pipelines with graphs in C).
agopen
– create a graphagnode
– create nodeagedge
– add edgetypedef struct Node {
int id;
struct Node* next;
} Node;
typedef struct {
Node** list;
int size;
} Graph;
Lightweight, embedded applications, and when external dependencies must be avoided.