element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Code Exchange
  • Technologies
  • More
Code Exchange
Blog Back to College: C Language Operators
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Code Exchange to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: phoenixcomm
  • Date Created: 30 Sep 2018 10:25 AM Date Created
  • Views 2298 views
  • Likes 8 likes
  • Comments 6 comments
  • Code Exchange
  • operators
  • back to college
  • arduino_tutorials
  • c programing language
Related
Recommended

Back to College: C Language Operators

phoenixcomm
phoenixcomm
30 Sep 2018

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).

  1. c is pre-incremented to 1
  2. then it adds b + c  and stores it in a or a = 0 + 1 so c is now 1
  3. then it post-increments a and b to 1
  4. 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.

  • Sign in to reply

Top Comments

  • Fred27
    Fred27 over 6 years ago +3
    I saw this exact thing over on eevblog.com recently. Was that you posting there too? Also, even if it was valid C, nobody should ever write really terse code like this. Some people do because they think…
  • phoenixcomm
    phoenixcomm over 6 years ago in reply to dixonselvan +2
    Thank you Dixon, you just have to remember that any of the group in this class is considered a unary operator where I said that x = i++ + i++; is not a valid construction as both sides of the operator…
  • shabaz
    shabaz over 6 years ago in reply to Fred27 +2
    Indeed, it doesn't matter to most users of languages, if it's legal with a 1990's or modern day compiler or not (this was the root of the original question that prompted this), because it would still get…
Parents
  • Fred27
    Fred27 over 6 years ago

    I saw this exact thing over on eevblog.com recently. Was that you posting there too?

     

    Also, even if it was valid C, nobody should ever write really terse code like this. Some people do because they think it makes them look clever. All it does is make it harder for the guy who has to debug or extend it later. The compiler doesn't care how slick your code looks and can often optimise more readable code just as well.

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • More
    • Cancel
Comment
  • Fred27
    Fred27 over 6 years ago

    I saw this exact thing over on eevblog.com recently. Was that you posting there too?

     

    Also, even if it was valid C, nobody should ever write really terse code like this. Some people do because they think it makes them look clever. All it does is make it harder for the guy who has to debug or extend it later. The compiler doesn't care how slick your code looks and can often optimise more readable code just as well.

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • More
    • Cancel
Children
  • shabaz
    shabaz over 6 years ago in reply to Fred27

    Indeed, it doesn't matter to most users of languages, if it's legal with a 1990's or modern day compiler or not (this was the root of the original question that prompted this), because it would still get thrown out of any code review (if it hadn't already been thrown out by an automated tool) and the coder would have to write it in a more appropriate way.

    I don't believe we ever interview-tested anyone with such things either.. usually they were extremely practical questions like "what do you do for maximum readability in your code" or "why do you program in C/X/Y" or "write a sample program that opens a file" etc..

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • phoenixcomm
    phoenixcomm over 6 years ago in reply to shabaz

    This is for both shabaz and Fred27. First, my code is as normally very terse. (That why I never liked overly flowery variable names, like that, are alluded or taught to you in PASCAL. I don't like some of some of the Java Coding Style. In my checkered life as an integration & test engineer, we normally don't have time for a "code review" You see if you have to review the code after well its too late. No, normally I write, or am given a specification, and then write the software from that. Then you test the code. About tools you can not and I stress this. You can not rely on a tool, or code reviews to produce 'good' code. No never. It is the knowledge of the guy who is writing the code to get the job done correctly. The tool (I use Eclipse) is to keep you honest, pick up typos, keep interfaces straight, etc.

    In real life I was asked this question i = i++ + i++; after squirming for about a minute or so. I informed my interviewer that this question was not valid where i = i++ + b++; was he continued to assure me that it was. after I thought about it for another minute or two I informed my interviewer well that was a little nuts and I did not want his job and left. A few weeks later his boss called me told me I was correct and hired me for a much better job.

    If this question is on a test beware TRAP! 

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube