simple program

This is a discussion on simple program within the C++ Programming forums, part of the General Programming Boards category; hi i am getting different answers for the same code in VC++ and TURBOC++ The source code is as follows ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    3

    simple program

    hi i am getting different answers for the same code in VC++ and
    TURBOC++

    The source code is as follows
    Code:
     void main()
     {
         int a=1;
         printf("%d",++a + ++a);
     }
    What is the clarification i can get.

    thanking you very much.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    http://c-faq.com/expr/index.html
    Your code is broken in more ways than there are lines of code, so what you get is entirely random and nothing to do with C.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Location
    Greece
    Posts
    52
    Code:
    int main()
     {
         int a=1;
         printf("%d", 2 * (a + 1));
    
         return 0;
     }
    The output is 4.

    Note: This program is C, not C++ (but you can compile it in a c++ compiler)

    EDIT: Salem speaks the truth.
    Last edited by myle; 07-15-2007 at 10:22 AM.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    . . . assuming you include stdio.h, of course.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a very simple program
    By htdefiant in forum C++ Programming
    Replies: 13
    Last Post: 08-14-2007, 01:27 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 02:42 PM
  4. simple silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 05:06 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM

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