Thread: Trouble with C algebra.

  1. #1
    Registered User
    Join Date
    Apr 2017
    Posts
    1

    Trouble with C algebra.

    Code:
    #include <stdio.h>
    
    int main(){
    
    int x = 10;
    
    printf("%d", x*=x--);
    
    return 0;
    }
    Why does this program display 90, shouldn't it display 99?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Expressions
    Because using a variable when it also has side effects is undefined behaviour.
    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
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Learn how to turn on compiler warnings. For your example, the compiler will help you. For example GCC says the following:

    Code:
    main.c:7:19: warning: operation on 'x' may be undefined [-Wsequence-point]
         printf("%d", x*=x--);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me with boolean algebra!!!
    By blakjakd in forum C++ Programming
    Replies: 4
    Last Post: 06-21-2010, 10:03 AM
  2. Algebra help
    By cyberfish in forum General Discussions
    Replies: 12
    Last Post: 10-24-2009, 02:42 AM
  3. Algebra Help!!
    By hero_bash in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-10-2006, 10:06 PM
  4. Algebra 1
    By Vicious in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 06-04-2002, 01:59 PM
  5. Algebra
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 04-10-2002, 09:51 PM

Tags for this Thread