Like Tree1Likes
  • 1 Post By Salem

Help me in simple programs

This is a discussion on Help me in simple programs within the C Programming forums, part of the General Programming Boards category; I have 2 problem-- Code: #include<stdio.h> void main() { int x=33,y=10; printf("%d"); } why printf always prints the last initialized ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    2

    Help me in simple programs

    I have 2 problem--
    Code:
    #include<stdio.h>
    void main()
    {
    int x=33,y=10;
    printf("%d");
    }
    why printf always prints the last initialized variable.

    Code:
    #include<stdio.h>
    void main()
    {
    int x=2,i;
    i=x++*2/++x*2;
    printf("%d",i);
    }
    how above expression is evaluated to 4..
    please give brief explanation.

  2. #2
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    1. Variables are passed to functions on the stack. Local variables are declared on the stack. The last initialised variable was the last thing pushed to the stack before the call to printf. (Probably, it could just happen that the same value was on the stack, you'd have to look at the assembler output to know 100%).

    2. The result of that expression is undefined. It could yield absolutely any result from 4 to opening up your disk drive or turning off your computer. It just happens to be 4 this particular time that you have run it.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    2
    thanks!! But in second program it always prints 4 no matter what the value of x has. i want to know -how??

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    1. Compiler should have given an error - you are not passing an expected argument. Output, if it compiles, is indeterminate.
    2. You may change the same variable twice because order of execution is not defined. Therefore output is indeterminate.

    I hope these are not examples given to teach C. They are clearly just plain wrong.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    > thanks!! But in second program it always prints 4 no matter what the value of x has. i want to know -how??
    Comma Operator - C And C++ | Dream.In.Code

    A lesson in English irregular verbs.
    "My compiler generates code which always prints 4"
    "Your compiler generates code which always crashes"
    "His compiler generates code which reformats the hard disk"

    Read this -> Expressions
    Until you know what a sequence point is, and what a side-effect is.

    Oh, and lose the "void main" habit as well, main returns an int.
    memcpy likes this.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Make simple programs start over.
    By Cooker2007 in forum C++ Programming
    Replies: 7
    Last Post: 01-16-2007, 03:08 PM
  2. Newbie! Help With Two Simple C++ Programs.
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 09-06-2005, 08:38 AM
  3. WIN API simple programs
    By Blizzarddog in forum Windows Programming
    Replies: 4
    Last Post: 03-24-2003, 01:58 AM
  4. Help me with these simple programs
    By Help me in forum C Programming
    Replies: 4
    Last Post: 11-08-2001, 09:38 AM
  5. Need help with simple programs...
    By BCole19 in forum C++ Programming
    Replies: 22
    Last Post: 08-30-2001, 09:45 PM

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