Thread: ++ Operator Misunderstanding

  1. #1
    Registered User hex_dump's Avatar
    Join Date
    Dec 2012

    ++ Operator Misunderstanding

    I have the following code
    #include <stdio.h>
    void show_str(const char *test_str);
    int main(void){
         char* the_str = "A woodchuck could chop as much as a woodchuck could\n";
         return 0;
    void show_str(const char *some_string){
        //using bracket notation
        int ndx = 0;
        //using increment operator
        while( (*some_string) ){
            putchar( *(some_string++) );
    doesn't the ++ operator have higher precedence, and thus *(string + 1) should always be the first element that is sent to stdout? As it stands it starts printing from (string + 0).

    It's been bothering me so if anyone can explain it another way that I'll I understand it would be appreciated.

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Melbourne Australia
    I think that you are trying to do this:
    putchar( *(++some_string) );
    You are incrementing the pointer after the line has completed, instead of before.


    banana = apple++ + mango;
    //is the same as
    banana = apple + mango;
    apple = apple + 1;
    banana = ++apple + mango;
    //is the same as
    apple = apple + 1;
    banana = apple + mango;
    Last edited by Click_here; 01-30-2013 at 04:30 PM.
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    Sep 2008
    Quote Originally Posted by hex_dump View Post
    doesn't the ++ operator have higher precedence... As it stands it starts printing from (string + 0).
    It does have higher precedence, but this is the correct behaviour for the code you posted. Remember that with postfix ++ (i.e. ++ after the operand like"some_string++") its operand is first used and then incremented - the increment only happens *after* its value is taken. With prefix ++, it is first incremented, then the (newly incremented) value is used.

    As a more concrete example:

    int post_a = 2;
    int post_b = post_a++;  // post_a++ yields value of post_a *before* it was incremented.
    // At this point, post_a = 3, post_b = 2.
    int pre_a = 2;
    int pre_b = ++pre_a;  // ++pre_a yields value of pre_a *after* it gets incremented.
    // At this point, pre_a = 3, pre_b = 3.
    Programming and other random guff: cat /dev/thoughts > (previously


    "The largest-scale pattern in the history of Unix is this: when and where Unix has adhered most closely to open-source practices, it has prospered. Attempts to proprietarize it have invariably resulted in stagnation and decline."

    Eric Raymond, The Art of Unix Programming

  4. #4
    Registered User
    Join Date
    Nov 2010
    Long Beach, CA
    Take a look here: C Operator Precedence Table. Pay particular attention to 'Note 2' at the bottom.

    To clarify more, yes, it has higher precedence, and your parentheses do ensure the ++ happens first, but you are misunderstanding the semantics of the ++ postfix operator.

    First, it may help to understand the concept of a sequence point (link). Basically, the increment happens at the end of a sequence point. A sequence point is the end of a full expression. Your parenthetical is not a full expression. You have a case more similar to rule #4: the call to putchar is the sequence point at which the increment happens, however the original value of some_string is passed to putchar.

    The result of the parenthetical expression
    is the current value of some_string, i.e. some_string+0. That value is used for the dereference operator *. Then, when you reach a sequence point, the increment happens. Basically what you have is equivalent to:
    putchar( *(some_string) );
    some_string += 1;
    Click_here gave you a possible solution if you wish to skip the first char, using the ++ prefix operator.

    Hope that clears it up a bit.
    Last edited by anduril462; 01-30-2013 at 05:13 PM.

  5. #5
    Registered User hex_dump's Avatar
    Join Date
    Dec 2012
    Excellent, thanks guys. Yes I think i was misunderstanding the semantics of the ++ operators and thank you very much for the links anduril462, much appreciated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. small misunderstanding please help
    By MyRedz in forum C++ Programming
    Replies: 1
    Last Post: 12-23-2009, 10:44 PM
  2. output misunderstanding
    By Neotriz in forum C Programming
    Replies: 3
    Last Post: 11-03-2009, 07:20 PM
  3. C Misunderstanding Of Variable Assignments.
    By Krash005 in forum C Programming
    Replies: 3
    Last Post: 09-07-2009, 05:29 PM
  4. Windsurfer Parabolic misunderstanding
    By cerin in forum Tech Board
    Replies: 7
    Last Post: 08-23-2009, 01:00 PM
  5. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
Website Security Test