r/cprogramming • u/Constant_Fox_5534 • May 25 '26
Some questions i have.
Hello,
I have some questions that i am really curious about.
- In this line, using the termios.h library:struct termios thing;
..
thing.c_lflag &= (something here)
Why do we use the '&=' operator? My first theory was because we need to store multiple values inside a single struct variable. Not really sure.
2. In what common cases fflush() function from the stdio.h header is used? I have only seen it to unbuffer an io stream so an output can come as soon as possible. But what if you pass a file stream instead of io streams in the first argument? What is that supposed to be used for, in that case?
3. In what cases can i use 'tmpnam/tempnam'? My first theory was using it to generate a random name for a temporary file.
Thanks for reading and possibly answering some of these. If i made a mistake, please tell me. (also sorry for the poor english)
4
u/Maleficent_Bee196 May 25 '26 edited May 26 '26
basically it. You use a byte as an array of boolean values. Basically, instead of reserve few bytes for each int that represents a boolean value, you can take 1 byte and each bit of it represents the state of something. You use the bitwise operators to set those bits to 0 or 1. For example, you can make a pokemon game and reserve a byte and each bit represents if the player have a badge or not. 00000001 tell me I have the first badge, but 00000010 tell me I have only the second badge.
3
u/flyingron May 25 '26
a &= b is roughly equivalent to a = a & b (except that a is only evaluated once). It's a common construct to clear bits in a integer.
thing.c_lflag &= ~IGNBRK;
This clears the IGNBRK bit. Similarly
thing.c_lflag |= IGNBRK;
sets the bit.
Termios doesn't use stdio. If you want to intersperse stdio calls, you need to fflush the stdio buffers before doing subsequent termios calls.
tmpnam is not part of termios, but it is indeed used to generate a UNIQUE (deterministic, not random) name for a temp file. It's officially deprecated because there's still a potential for a race condition between when the unique name is generated and when your process creates one, another process may create the the same name. This is why there are functions that combine the name generation and file creation like mksemp and tempfile.
3
u/un_virus_SDF May 27 '26