--- categories: - docs - develop - stack - oss - rs - rc - oss - kubernetes - clients description: Connect your C application to a Redis database. linkTitle: hiredis (C) title: hiredis guide (C) weight: 10 --- [`hiredis`](https://github.com/redis/hiredis) is the [C language](https://en.wikipedia.org/wiki/C_(programming_language)) client for Redis. The sections below explain how to install `hiredis` and connect your application to a Redis database. `hiredis` requires a running Redis or [Redis Stack]({{< relref "/operate/oss_and_stack/install/install-stack/" >}}) server. See [Getting started]({{< relref "/operate/oss_and_stack/install/" >}}) for Redis installation instructions. ## Build and install Clone or download the `hiredis` source from the [Github repository](https://github.com/redis/hiredis). Then, in a terminal, go into the `hiredis` folder and run the `make` command to build the dynamically-loaded library for `hiredis` (this has the name `libhiredis.dylib` on MacOS and `libhiredis.so` on Linux). You can copy this library to your project folder or run `sudo make install` to install it to `/usr/local/lib`. ## Connect and test The code in the example below connects to the server, stores and retrieves a string key using [`SET`]({{< relref "/commands/set" >}}) and [`GET`]({{< relref "/commands/get" >}}), and then finally closes the connection. An explanation of the code follows the example. {{< clients-example set="landing" step="connect" lang_filter="C" description="Foundational: Connect to a Redis server, set and retrieve string values using SET and GET, then close the connection" difficulty="beginner" >}} {{< /clients-example >}} For a real project, you would build your code with a makefile, but for this simple test, you can just place it in a file called `main.c` and build it with the following command. (If you didn't install `hiredis` using `make install`, then you should also use the `-I` option to specify the folder that contains the `hiredis` headers.) ```bash cc main.c -L/usr/local/lib -lhiredis ``` The default executable filename is `a.out`. If you run this file from the terminal, you should see the following output: ``` % ./a.out Reply: OK Reply: bar ``` The code first uses `redisConnect()` to open the connection for all subsequent commands to use. See [Connect]({{< relref "/develop/clients/hiredis/connect" >}}) for more information about connecting to Redis. The `redisCommand()` function issues commands to the server, each of which returns a `redisReply` pointer. Here, the reply is a string, which you can access using the `str` field of the reply. The `redisCommand()` call allocates memory for the reply, so you should free this with `freeReplyObject()` when you have finished using it. See [Issue commands]({{< relref "/develop/clients/hiredis/issue-commands" >}}) and [Handle replies]({{< relref "/develop/clients/hiredis/handle-replies" >}}) for more information. Finally, you should close the connection to Redis with a call to `redisFree()`. This is not strictly necessary for this short test program, but real-world code will typically open and use many connections. You must free them after using them to prevent errors. ## More information The [`hiredis`](https://github.com/redis/hiredis) Github repository contains examples and details that may be useful if you are using `hiredis` to implement a higher-level client for another programming language. There are also examples showing how to use `hiredis` adapter headers to integrate with various event handling frameworks. See the other pages in this section for more information and examples.