Meaning of Statements int x;{/*something*/}

This code is part of a semaphore code, but what does line 2 until the end mean?
Are they treating this part as an structure?
I assume that int sid; has no purpose. Any ideas?
Thank you!

1
2
3
4
5
static void P(sid)
int sid;
{
    semcall(sid, -1);
}
'int sid;' is just a normal declaration of integer variable sid. The ';' should tip you off.

What the wrapping of the code in {}'s does, is that the code only 'lives' between the {}'s.

I don't see why it would make any difference here, but for example, if you would declare a variable in between those {}'s, and use that same variable later outside the {}'s, the variable would be out of scope.
Last edited on
closed account (3CXz8vqX)
Question...variable called in P(sid) yet it hasn't been initialised.... >.> I smell a compile time bug.

knowNothing. It is just a variable declaration. It's in a really interesting place however.
Ow lol, didn't even see that one, because I got distracted by the weird use of brackets. The integer 'sid' is declared AFTER it is 'used'. You need to put line 2 before the first line.
So, basically omit int sid; from the method?
Last edited on
In C, these two are equivalent:

1
2
3
4
5
6
int plus( arg1, arg2 )
int arg1 ;
int arg2 ;
{
    return arg1 + arg2 ;
}


1
2
3
4
int plus( int arg1, int arg2 )
{
    return arg1 + arg2 ;
}


In C++, the first version is a compile-time error.
Thank you, I should have mentiioned that I am using C.
Topic archived. No new replies allowed.