I will not stutter. C is one of the most powerful computer languages ever written. In this blog, I will show what an operator is.
An operator in its simplest form would be: a added to b. In this case, the operator is added to. But you also have an assignment operator in its simplest form would be a assigned to b. In this case, the operator is assigned to.
But in C we also have a compound opeator. In most cases they are used every day they are a rich set of operators. Such as ++i, (pre-inc), i++ (post-inc) and as well as +=, -=, *=, /=, and %=.
So the proper use of a compound operator += would be a += b is the same as a = a + b. In the case of the increment operator, may be used on either side of the equation. So i++ is saying i = i + 1, that is incrementing the value of i. i++ of course has its true opposite i -- where i = i - 1. So in fact this is quite legal: a++ = b++ + ++c; So lets apply some numbers to this a, b, c are all going to be 0. as the compiler evaluates this mess (this is a extreem example and hope never to see it in the wild).
- c is pre-incremented to 1
- then it adds b + c and stores it in a or a = 0 + 1 so c is now 1
- then it post-increments a and b to 1
- final values are a = 2, b = 1, c = 1.
The Railroad Tracks or BNS (Backus-Naur Form) for the C Language
This grammar was adapted from Section A13 of The C programming language, 2nd edition, by Brian W. Kernighan and Dennis M. Ritchie,Prentice Hall, 1988.
<unary-expression> ::= <postfix-expression> | ++ <unary-expression> | -- <unary-expression> |
<unary-operator> <cast-expression> | sizeof <unary-expression> | sizeof <type-name>
<postfix-expression> ::= <primary-expression> | <postfix-expression> [ <expression> ] |
<postfix-expression> ( {<assignment-expression>}* ) | <postfix-expression> . <identifier> | <postfix-expression> -> <identifier> | <postfix-expression> ++ | <postfix-expression>
<unary-operator> ::= & | * | + | - | ~ | !
<expression> ::= <assignment-expression> | <expression> , <assignment-expression>
<assignment-expression> ::= <conditional-expression> | <unary-expression> <assignment-operator> <assignment-expression>
<assignment-operator> ::= = | * | /= | %= | += | -= | <<= | >>= | &= | ^= | |=
This information is given to any student in school who learns C. For instance by reading this chapter of the book you know that C does not have a square operator such as other languages (sometimes written as ** or ^) so what do we as C programmers do? Hey let the Pre-Processor do it:
#define sq(x) ((x)*(x)) and that it. You could also have done this as a function. Why the Pre-Processor and not a function: works on any data type int, float the compiler never sees it and best there is no CALL/RETURN on the stack.
Abuse and Tests:
x= ++i + ++i + ++i;
This is a question that I commented on earlier. The person giving the test knows that your answer should be "This is not legal C Language syntax", If you try an answer this hot mess he knows that you do not know C that well. To put this in perspective I never took the Introduction to The C Language.
Top Comments