How Does Increment Operators Work...??

This is a discussion on How Does Increment Operators Work...?? within the C Programming forums, part of the General Programming Boards category; CAN ANY1 PLZ TELL ME HOW INCREMENT & DECREMENT OPERATORS WORK IN C AS WELL AS IN LINUX IF KNOWN....!! ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Location
    PUNE
    Posts
    23

    Question How Does Increment Operators Work...??

    CAN ANY1 PLZ TELL ME HOW INCREMENT & DECREMENT OPERATORS WORK IN C AS WELL AS IN LINUX IF KNOWN....!!
    AS FAR AS I KNWO THERE ARE POST & PRE INCREMENT/DECREMENT OPERATORS :
    I M GETTING DIFF RESULTS WHEN CALCULATED MANUALLY & DIFFERENT WHEN PROGRAMMED IN C ....!!
    SUPPOSE FOR EX :

    a = 2b + (- - b) + 3(b++) +b

    if b = 3
    what is ans of b
    manually ans is = 6 + 2 + (3*2) + 3 = 17
    but when programmed ans is 19 yyyyyyy..........???
    nt only in this case in many cases it happens i want to know the priorities which comes 1st and all ThX in adv....!!

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    It's undefined behavior as the first couple topics here address:
    http://c-faq.com/~scs/cgi-bin/faqcat.cgi?sec=expr

    More:
    http://www.embedded.com/story/OEG20020625S0041
    Last edited by hk_mp5kpdw; 02-06-2008 at 07:10 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The order that b++ and --b are performed in relation to the other operations in the expression is undefined, so there is no gurantee that the compiler won't do 3 *3 first, then the --b operation. In particular, using two different increment/decrement operators in the same line of code is not defined behaviour for C and C++.

    The general rule is "avoid using increment and decrement operators" in complex expressions, because you can't know for sure what happens. Note that it may well calculate the right result on another compiler, and come up with a different incorrect result on a third compiler.

    It is also better for the compiler to do something like this:
    Code:
    a = 2*b + (b-1) + 3*(b-1) +b
    because the compiler can easily calculate all those +1 and -1 operations into one caculatlation, so it becomes:
    a = 2 *b + b + 3 * b - 4 + b;
    We can then optimize this a bit further:
    a = 7 * b - 4
    And we get the correct result on ANY compiler.


    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    And don't use caps.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Location
    PUNE
    Posts
    23
    how about this...!!

    Code:
    #include<stdio.h>
    	main()
    	{
    	int a,b,s;
    	printf("Enter the value of s\n");
    	scanf("&#37;d",&s);
    	b=s;
    	a=2*b+(--b)+(3*(b++))+b;
    	printf("The value of a is %d\n",a);
    	}
    manually ans i m getting is : when b = 3
    a = (2*3) + (2) + ( 3 * 2) + 3 = 17

    when programmed ans m gettin is 14
    & sometimes 19 pl chk

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    Did you read anything we just wrote? It's undefined behavior!
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    Feb 2008
    Location
    PUNE
    Posts
    23
    oh k thX matsp

    act i was posting tht program bfore i read ny thng posted by u ppl ThX

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    Main returns int.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Feb 2008
    Location
    PUNE
    Posts
    23
    then pl tell me how to solve this : using c program....!!

    Code:
    a=2*b+(--b)+3*(b++)+b

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What do you want to "solve". I just wrote a different version that is definitive in the second post. Just avoid using ++ and -- in the expression.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    You really are clueless, aren't you? Even though we told you.
    Do not use increment or decrement operators in complex expressions!
    --n and n-- just decrements the value by one and ++n and n++ just increments the value by one, so rewrite it!

    a = 2 * b + (b - 1) + 3 * (b + 1) + b
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Feb 2008
    Location
    PUNE
    Posts
    23
    yes now i got the Logic thX a lot matsp & Elysia

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    a = 2 * b + (b - 1) + 3 * (b + 1) + b
    That is incorrect. I posted the correct variant in post #3. Note that the third reference to b is "b++", so it takes "b-1" derived from --b, then restores b's value.
    I also noted that we can just as well write:
    a = 7*b - 4

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Registered User
    Join Date
    Feb 2008
    Location
    PUNE
    Posts
    23
    can ny1 xplain in detail step by step plz....!!

  15. #15
    Registered User
    Join Date
    Feb 2008
    Location
    PUNE
    Posts
    23
    is that we replace n- - or - -n by n-1
    replace n++ or ++n by n+1

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  2. Problems in getting OpenGL to work
    By zonf in forum C Programming
    Replies: 5
    Last Post: 02-13-2006, 03:48 AM
  3. Why won't my OpenGL work?
    By Raigne in forum C++ Programming
    Replies: 7
    Last Post: 11-26-2005, 10:53 AM
  4. Developers Wanted
    By Quasicom in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 08-24-2005, 12:46 AM
  5. Overloading Operators
    By Strahan in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2001, 11:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21