I found a strange use of a block in the definition of a C function (in the source of a dynamic window manager).
It's a block inside the definition of a function. Line 944 of this file has an example. What is this about?
void
grabbuttons(Client *c, Bool focused) {
updatenumlockmask();
{
unsigned int i, j;
unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
//some more code
}
}
It is simply that: a block. It introduces a new limited scope: variables declared inside aren't usable outside, so it can be used to limit the scope of a set of variables.
But often it's just used to organize code for readability, and perhaps to suggest or remind of some additional details (or simply to force an additional level of indentation from your editor), e.g.:
lockDatabase();
{
// this code is all within the database lock:
}
unlockDatabase();
Furthermore, older C standards limited variable declarations to the beginning of a block only. Under that restriction, your choices are to declare all of your variables at the beginning of your function or other (blocked) control structure, or to introduce a new bare block solely for this purpose of declaring additional variables.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With