Thread: Strange behaviour of printf

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    30

    Question Strange behaviour of printf

    Hello every one I dont understand why my output is different
    here is my code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct {
    	int len ;   //cannot initialize to a value directly
    	char *str;
    	int a;
    } *p;
    //size of pointer is always constant = 4
    
    main(){
    	p = malloc(sizeof(p));
    	p->len = 0;
    	printf("%d\t%d",p->len,p->len++);
    }
    Expected output
    0 0

    original output
    1 0

    and when write almost similar code like this one
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct {
    	int len ;   //cannot initialize to a value directly
    	char *str;
    	int a;
    } *p;
    //size of pointer is always constant = 4
    
    main(){
    	p = malloc(sizeof(p));
    	p->len = 0;
    	printf("%d\t%d",p->len,p->len+1);
    }
    I get the out put as
    0 1
    which is expected

    can any one clarify my doubt, is it some thing related to precedence of '++' then why does printf print the first argument as 1 and latter as 0

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You have no guarantee when ++ happens, except "before the end of the line". It might happen before the other p->len argument to printf is evaluated, or it might not, depending on how the people who write the compiler want to do it.

    Also, you get extremely lucky as your malloc does not allocate nearly enough storage for an entire struct -- it only gets enough memory for a pointer to the struct. You should malloc sizeof(*p) instead.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    30
    i have analyzed the cases with
    Code:
    	printf("%d\t%d\t%d\n",p->len++,p->len++,p->len++);
    the out put i get is
    2 1 0

    and even with
    Code:
    p->str = "what is it";
    printf("%c\t%c\t",*p->str,*p->str++);
    out put
    h w
    i get the reverse way in all the cases

    yeah the sizeof(*p) was my mistake sorry

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    The order function arguments are evaluated is undefined. It can happen in any order. Thus, modifying a variable more than once in the same expression can lead to problems. So can reading and modifying the same variable in the same expression, unless the variable is used to evaluate it's own new value. The expection is operators &&, ||, ,(comma), and ?:.
    Last edited by King Mir; 08-29-2009 at 10:45 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    30
    thank you...

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Read this -> Expressions
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  2. segmentation fault upon reload
    By yabud in forum C Programming
    Replies: 8
    Last Post: 12-18-2006, 06:54 AM
  3. Simple C question: user input to repeat a loop
    By evernaut in forum C Programming
    Replies: 2
    Last Post: 11-18-2006, 09:23 AM
  4. Whats Wrong Whith This!?
    By SmokingMonkey in forum C++ Programming
    Replies: 8
    Last Post: 06-01-2003, 09:42 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM