Hi All, I am newbie in this. Is this function declaration right, as it has bit as parameter, void myfunc(unsigned char,bit);
Ashutosh
No, it isn't.
You really need to get a good 'C' textbook (or books) to learn the basics of the 'C' programming language.
Some suggestions here: http://www.keil.com/books
You may also let the compiler decide, if its right or not. If your code is invalid it will give you an error.
In C51 it's legal - Or at least, it's accepted.
Try compiling this (non-functional) snippet:
void myfunc(unsigned char,bit); void main(void) { myfunc(1,1); } void myfunc(unsigned char a,bit b) { if (b) ++a; }
No, the compiler will not detect if it's right or not - ie, if it does what the programmer intended - the compiler will just determine whether it is valid syntax or not.
The classic example is, of course:
// Test if 'a' is equal to 'b' if( a = b ) { // The test is valid syntax, but does not match the // programmer's intention stated in the comment! }
Yes, this is such a common mistake that many (most?) compilers today will warn about it - but compilers cannot possibly recognise every mis-application of correct syntax!
http://www.keil.com/support/man/docs/c51/c51_le_bit.htm
It's completely standard 'C'.
Here's a quiz:
int Test(void) { int a; return sizeof a; }
Who reached for their favourite 'C' book?
At least 99.999% of existing code would not look like that ;)
Is it - even with 'bit' ?
However, the fact that it's syntactically valid in isolation doesn't mean that it is "right".
To be "right" means that the declaration has to match the definition. As we have no idea what the definition is, the balance of probabilities that one declaration matches one of the infinite space of possible definitions must be vanishingly small. So "No" is the most-likely-correct answer...
;-)
Good catch!
Your original post was a perfectly correct answer to the OP's question. If he'd asked a different question, your answer would have been wrong - nonetheless, the code he posted is standard 'C' (this time with the proviso that it uses implementation specific extensions as permitted by the standard).
Did I get away with that?
Make that 100% round here...
It is very long between each time I see anyone use sizeof as an operator instead of treating it as a function.
Out of "compatibility", I use it as a function just to avoid questions.
... I use it as a function just to avoid questions.
On the other hand, the non-parenthesized version, when used, is an immediate clue that sizeof's operand is not a type. It is a subjective matter whether an individual or shop puts any value on that language feature.