# C Usage

## Usage Instructions for C

See the release bundle for some examples of C programs which can be compiled and run on Valida. Here is an example C program from this repo, called `cat.c`:

```
#include <stdio.h>

const unsigned EOF_ = 0xFFFFFFFF;

int main() {
    unsigned c = 0;
    while (1) {
        c = getchar();
        if (c == EOF_) {
            break;
        } else {
            putchar(c);
        }
    }
}
```

To compile the `cat.c` example to Valida from within the Valida shell or the Valida Docker image shell:

```bash
clang -target valida /valida-toolchain/example/c/cat.c -o cat
valida run cat log
```

Once running, the `cat` example will wait for input. After you are done providing input, press `ctrl+D`. The program should echo back what you wrote, writing its output to `log`.

Compiling and running the other examples follows the same procedure, substituting `$NAME` for the name of the example:

```bash
clang -target valida /valida-toolchain/examples/c/${NAME}.c -o ${NAME}
valida run ${NAME} log
```

### C standard libraries

See [Using LLVM libc](broken://pages/Rp0KOncdE4z5cwO0AoLX) for instructions on how to use the libc support included with the Valida toolchain. This allows for the use of some libc header files and standard library functions and macros.

### Floating point support

The LLVM Valida compiler toolchain supports floating point types via software emulation. All of the normal arithmetic operations on floating point are supported. `fenv.h` is not supported. It is recommended to avoid using floating point types in Valida, because it's not very efficient to do it using software emulation.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lita.gitbook.io/lita-documentation/quick-start/valida-compiler-toolchain/c-usage.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
