You can also create for example the following Bash script, make it executable (chmod a+rx script), move it somewhere along your PATH (I use $HOME/bin/), so that e.g.
script '(uint16_t)((float)(85) / ((float)(0x9580) - (float)(0x7c30)) * 65536.0f + 0.5f)'
outputs 860 nicely without any hassles. Just remember to use single quotes to stop the shell from expanding or interpreting the string.
The script itself:
#!/bin/bash
# SPDX-License-Identifier: CC0-1.0
if [ $# -ne 1 ] || [ ":$1" = ":-h" ] || [ ":$1" = ":--help" ]; then
exec >&2
printf '\n'
printf 'Usage: %s [ -h | --help ]\n' "$0"
printf ' %s C-statement\n' "$0"
printf '\n'
exit 1
fi
Work=$(mktemp -d) || exit 1
trap "rm -rf '$Work'" EXIT
(
printf '#define _POSIX_C_SOURCE 200809L\n'
printf '#define _GNU_SOURCE\n'
printf '#include <stdlib.h>\n'
printf '#include <inttypes.h>\n'
printf '#include <string.h>\n'
printf '#include <stdio.h>\n'
printf '#include <math.h>\n'
printf 'void print_u(const unsigned long long u) { printf("%%llu\\n", u); }\n'
printf 'void print_i(const long long i) { printf("%%lld\\n", i); }\n'
printf 'void print_ld(const long double ld) { printf("%%.18Lf\\n", ld); }\n'
printf 'void print_d(const double d) { printf("%%.12f\\n", d); }\n'
printf 'void print_f(const float f) { printf("%%.6f\\n", f); }\n'
printf 'void print_s(const char *const s) { printf("\\"%%s\\"\\n", s); }\n'
printf 'void print_c(const char c) { if (c>=32 && c<=126 && c!=39) printf("\\047%%c\\047\\n", c); else printf("(char)%%d\\n", c); }\n'
printf '#define PRINT(expr) (_Generic((expr), unsigned long long: print_u, unsigned long: print_u, unsigned: print_u, long long: print_i, long: print_i, int: print_i, long double: print_ld, double: print_d, float: print_f, char *: print_s, char: print_c, default: print_i)(expr))\n'
printf 'int main(void) { PRINT(%s); return EXIT_SUCCESS; }' "$1"
) > "$Work/source.c"
gcc -Wall -Wextra -O2 -std=c11 "$Work/source.c" -lm -o "$Work/executable" || exit 1
"$Work/executable" || exit 1
exit 0
and is licensed under the Creative Commons 1.0 license per the SPDX license identifier: do whatever you like with it at all, just don't blame me for any breakage or bugs.
The real trick in the script is the use of mktemp -d and trap "..." EXIT to make an auto-removed work directory that is always removed when the script exits, even if it is interrupted or exits due an error.
It does use the C11 and later _Generic keyword to select the proper printf pattern to use for outputting the value of the expression based on the type of the expression, so based on your needs, you might wish to add new types, or modify the type-specific print statements.
This handles even obscure C syntax correctly:
script '2["foo"]'
outputs
'o'