Thread: simple program

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

  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,057
    . . . 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, 06:06 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM