Thread: C compiler reads from right to left or left to right?

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    2

    C compiler reads from right to left or left to right?

    C compiler reads from right to left or left to right?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    What's that supposed to mean.

    Are you talking about
    - source code
    - expressions
    - parameter passing
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ajishgopalr View Post
    C compiler reads from right to left or left to right?
    Yes.

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by CommonTater View Post
    Yes.
    Haha.... lets not forget about top down.

    @OP This question is a bit much for a thread topic. If you are actually interested in compiler design, which will allow you to understand how the C compiler works, a good starting point for beginners is a series of articles that were written by Jack Crenshaw entitled - Let's Build a Compiler.

    Otherwise as Salem pointed out you will need to ask a much more specific question, e.g. how does the C compiler interpret....<this specific example>
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    There no answer like 'YES' or 'NO' to this question. Its much more complicated than that to explain the whole lot. But from the lexical parsing point of view 'yes' it is left to right, but then later on the tokenised code it sent to syntax analyser which then becomes much more complicated in term like checking the grammar, stack machine and the rest.

    ssharish
    Last edited by ssharish2005; 07-16-2011 at 10:48 AM.
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  6. #6
    Registered User
    Join Date
    Jul 2011
    Posts
    2
    Can anyone help me understand the code below:

    Code:
    int main()
    {
    int var=7;
    printf("%d", var++ * ++var);
    return 0;
    }

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    No, because the code below isn't valid. It's not defined which side of * is evaluated first, and at what point a post-increment will happen (other than "before the start of the next line"). Therefore you can get many different possible outputs.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ajishgopalr
    Can anyone help me understand the code below:
    Ah, so this program is the underlying reason for your question in this thread?

    Read this FAQ: comp.lang.c FAQ list · Question 3.2
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ajishgopalr View Post
    Can anyone help me understand the code below:

    Code:
    int main()
    {
    int var=7;
    printf("%d", var++ * ++var);
    return 0;
    }
    Best bet... run it and see what happens. Change things a bit... run it again, until you understand it.

  10. #10
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Best bet... run it and see what happens. Change things a bit... run it again, until you understand it.
    May not be the best idea when dealing with undefined behaviour.

    Like tabstop said, this is undefined. Just because you get one answer from your implementation doesn't mean you'll always get it.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I wonder, in written languages that read from right to left, if they can make source code that is right to left. I mean we use left to right, so the compiler does, so we write "int" instead of "tni". But I wonder - because the language itself shouldn't actually care - if you could have a right to left compiler. I mean I guess you could. You could always swap out a different character to represent 'int', right? Hm, I never thought about that. Might be interesting to see.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Anything is possible as long as the parser for the compiler is written for it. I mean he*l, they even made a whitespace programming language.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by quzah
    I wonder, in written languages that read from right to left, if they can make source code that is right to left. I mean we use left to right, so the compiler does, so we write "int" instead of "tni". But I wonder - because the language itself shouldn't actually care - if you could have a right to left compiler. I mean I guess you could. You could always swap out a different character to represent 'int', right? Hm, I never thought about that. Might be interesting to see.
    Back in school, I learned to read and write Chinese (or more accurately, Mandarin) characters from left to right, top to bottom, just like English. However, traditionally, the characters are written top to bottom, right to left. If you want to be funky, you could have variants like bottom to top, left to right, or right to left, bottom to top.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Left()
    By cbrandy in forum C Programming
    Replies: 1
    Last Post: 02-20-2011, 05:28 AM
  2. Left and Right Click
    By EdBier in forum C++ Programming
    Replies: 1
    Last Post: 05-13-2009, 10:54 PM
  3. address of left son..
    By transgalactic2 in forum C Programming
    Replies: 7
    Last Post: 04-21-2009, 03:06 PM
  4. left operand must be l-value
    By Bill83 in forum C++ Programming
    Replies: 14
    Last Post: 10-11-2006, 10:42 AM
  5. Left Align
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 04-29-2002, 07:47 PM