How to produce a NaN float in c?

Using floating point numbers, 0.0 / 0.0 isn’t a “divide by zero” error; it results in NaN. This C program prints -nan: #include <stdio.h> int main() { float x = 0.0 / 0.0; printf(“%f\n”, x); return 0; } In terms what NaN looks like to the computer, two “invalid” numbers are reserved for “signaling” and … Read more

How do I implement a circular list (ring buffer) in C?

A very simple implementation, expressed in C. Implements a circular buffer style FIFO queue. Could be made more generic by creating a structure containing the queue size, queue data, and queue indexes (in and out), which would be passed in with the data to add or remove from the queue. These same routines could then … Read more

What does static mean in ANSI-C [duplicate]

Just as a brief answer, there are two usages for the static keyword when defining variables: 1- Variables defined in the file scope with static keyword, i.e. defined outside functions will be visible only within that file. Any attempt to access them from other files will result in unresolved symbol at link time. 2- Variables … Read more

how to bind raw socket to specific interface

const char *opt; opt = “eth0”; const len = strnlen(opt, IFNAMSIZ); if (len == IFNAMSIZ) { fprintf(stderr, “Too long iface name”); return 1; } setsockopt(sd, SOL_SOCKET, SO_BINDTODEVICE, opt, len); First line: set up your variable Second line: tell the program which interface to bind to Lines 3-5: get length of interface name and check if … Read more

tech