Thread: Why so..

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    385

    Why so..

    Code:
    #include<stdio.h>
    #include<string.h>
    
    
    void sort(char *b[]);
    
    int main()
    {
    	char *b = "asdfjh";
    	char c;
    
    	printf("\n%c ",*b++);
    	printf("\n%c ",*b--);
    	printf("\n%c ",*b);
    
    }
    If first printf prints out the first character ('a') and then increments b to 's' such that the second printf prints out 's' and again decrements b to 'a', then isn't the third printf supposed to print out 'a'...

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Yep.
    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.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    ...why is it not happening??

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    It works for me:
    C code - 16 lines - codepad
    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
    Dec 2011
    Posts
    9
    hi..
    in *b++,in which * and ++ have same precedence but associativity is from right to left.
    so *b++,incrementing is done after ; in the line..and b now a pointer containing the starting address of "asfdj".so taking star will give you the 1st element ie a.
    in next line the b value is incremented due to the post increment in the previous ,so that you will get output as 's'. after which your post decrement is happening.so that in next printf you get 'a' output..

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    wonder if somethings wrong with my pelles C...
    works in MSVC 2010...

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    "oops..." has it right.

    Out of curiosity, what did pelles C do with it?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Quote Originally Posted by MK27 View Post
    "oops..." has it right.

    Out of curiosity, what did pelles C do with it?
    output with pelles c (or, atleast, "my pelles C"):
    a
    s
    (blank)

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by juice
    output with pelles c (or, atleast, "my pelles C"):
    a
    s
    (blank)
    Hmm... just in case you misread the output, try:
    Code:
    #include<stdio.h>
    
    int main(void)
    {
        const char *b = "asdfjh";
    
        printf("%c ", *b++);
        printf("%c ", *b--);
        printf("%c\n", *b);
    
        return 0;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Quote Originally Posted by laserlight View Post
    Hmm... just in case you misread the output, try:
    Code:
    #include<stdio.h>
    
    int main(void)
    {
        const char *b = "asdfjh";
    
        printf("%c ", *b++);
        printf("%c ", *b--);
        printf("%c\n", *b);
    
        return 0;
    }
    This time....
    a s (blank)

  11. #11
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Is it happening only with me, or is it the same for anyone who uses pelles C....

  12. #12
    Registered User joybanerjee39's Avatar
    Join Date
    Oct 2011
    Location
    kolkata
    Posts
    106
    Quote Originally Posted by juice View Post
    This time....
    a s (blank)
    it works fine with my pelles c !

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by juice View Post
    wonder if somethings wrong with my pelles C...
    works in MSVC 2010...
    On trying it here in Pelles C (650 rc4 x64)...

    Go into Project -> Project Options -> Compiler -> Optimizations = None

    Then it will work without warnings.

    However... the code itself is probably what the picky police here would call "undefined behaviour" since you are messing with the root pointer to a string...

  14. #14
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    what laserlight said. with some standard libraries you have to have a line feed to flush the output buffer or it won't print. gcc on linux is that way. don't know about pelles on windows

  15. #15
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Quote Originally Posted by CommonTater View Post
    On trying it here in Pelles C (650 rc4 x64)...

    Go into Project -> Project Options -> Compiler -> Optimizations = None

    Then it will work without warnings.

    However... the code itself is probably what the picky police here would call "undefined behaviour" since you are messing with the root pointer to a string...
    Thanks... Its working now.. I wonder what was wrong.

Popular pages Recent additions subscribe to a feed