Thread: Some Basic questions

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    43

    Some Basic questions

    Code:
    Operators 	                                           Associativity 
    
    () [] -> .	                                                   left to right
    ! ~ ++ -- + - * (type) sizeof	                       right to left
    * / % 	                                                       left to right
    + - 	                                                           left to right

    I was reading the 2nd chapter of K&R's The C programming language and came up accross this table.What i mentioned above is just a part of it.

    I was confused by the 2nd ,3rd and 4th rows. As we can see in the second we have +-*
    and in the 3rd we have * and in the 4th +-. Why the same thing is shown in different rows? Why is their Associativity different ?
    And what is meant by associativity.Is it that if the same operator is available in the same expression ?

    As i continued reading i came upon

    x = f() + g();

    where it said that the result is unpredictable.But wouldnt it go from left to right ? It should first it calculates the f() and then g() ... ?

    Im confused with all of these ... Any help would be great .Thank you

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by BlaX View Post
    I was confused by the 2nd ,3rd and 4th rows.
    The 2nd row are the unary operators, which form an expression with only one other element. This includes post/pre-increment (eg, i++ or --i). So that's different than the math operators + and - in line 4. Unary * is dereference, not multiply.

    The math operators are "binary", ie, they need two other elements to form an expression.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The result may well be predictable, but whether f() is evaluated first or g() is evaluated first is not predictable given just the source code.
    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

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    So here goes my doubt. How's the +,- of 2nd row different from +,- of the 4th row. Can anybody give an example where they are different. How can +,- be used as unary operators?
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    a = -1;
    Unary minus

    a = +1;
    Unary plus
    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.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by BEN10
    How can +,- be used as unary operators?
    By definition of "unary operator" and "left to right" associativity: +x and -x are such examples.
    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

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Or, even better:
    a = -b; /* unary minus */
    etc.

  8. #8
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Ok, thanks to all. Now I got it.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic Questions
    By Panda_ in forum C Programming
    Replies: 15
    Last Post: 09-23-2009, 04:24 PM
  2. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  3. Please help, basic unsigned conversion questions
    By ninjacookies in forum C Programming
    Replies: 3
    Last Post: 04-20-2005, 10:50 AM
  4. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM