difference b/w, wordcnt++ and wordcnt+1

Discussion in 'Programmer's Corner' started by engrrehmat, Feb 5, 2013.

  1. engrrehmat

    Thread Starter New Member

    Dec 17, 2010
    26
    0
    what is the difference between wordcnt++ and wordcnt+1, both are increment, but i am confused with their operations.
    any one kindly make me understand how they both works and what is the output, the output is different i have tried.
     
  2. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    wordcnt++ is a post increment and will result in the value changing after the line it is on. Moreover, it returns a new object with the copied value, meaning it will be slower compared with ++wordcnt(pre-increment), wich returns the current(incremented) object's value...

    wordcnt+1 returns the value of...well, exactly that. it doesn't affect the value left in wordcnt.

    This is most likely your problem, if you want specific help, post a code snippet...
     
  3. engrrehmat

    Thread Starter New Member

    Dec 17, 2010
    26
    0
    in this program, it works fine, but when i replace wordcnt+1 with wordcnt++, it doesnt count the exact number of words.

    int main()
    {
    int charcnt=0;
    int wordcnt=0;
    char ch;

    printf("type in a phrase:");
    while((ch=getche())!='\r')
    {
    charcnt++;
    if(ch==' ')
    wordcnt++;
    }
    printf("\nis:%d",charcnt);
    printf("\nis:%d",wordcnt++);
    getch();
    }
     
  4. MrChips

    Moderator

    Oct 2, 2009
    12,449
    3,365
    wordcnt+1
    by itself does nothing.

    You mean:

    wordcnt = wordcnt + 1;
     
  5. engrrehmat

    Thread Starter New Member

    Dec 17, 2010
    26
    0
    in the program,
    in the printf("word is:%d",wordcnt+1);

    in this statement how wordcnt+1 works,
     
  6. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    You need to use CODE tags, it is in the toolbar and looks like '#':
    Code ( (Unknown Language)):
    1. printf("\nis:%d",wordcnt++);
    Like I said, this is a post-increment, wordcnt doesn't get incremented until after it's value is printed....
     
  7. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    use ++wordcnt
     
  8. MrChips

    Moderator

    Oct 2, 2009
    12,449
    3,365
    Neither. Use:

    Code ( (Unknown Language)):
    1.  
    2. printf("word is:%d",wordcnt);
    3.  
     
  9. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    True, I am speaking strictly from the numerical point of view. The word count should already be at the actual word count before it is printed....
     
  10. Papabravo

    Expert

    Feb 24, 2006
    10,155
    1,795
    The post increment operator is more general than the binary(two operands, not base 2) addition operator. If the object is a pointer to char, the value of the pointer is increased by 1. If it is a pointer to int then it is increased by sizeof(int). If it is a structure then it will increase by the sizeof(struct).
     
  11. WBahn

    Moderator

    Mar 31, 2012
    17,775
    4,804
    No, they are equivalent in that sense.

    If p is a pointer to a value of type double, then p++, p+=1, and p=p+1 all have the same effect. The sum of a pointer and an integer evaluates to the value of the pointer incremented by the value of the integer (which can be negative) multiplied by the size of the object pointed to by p. This is why you can't do pinter arithmetic with void pointers.

    A compiler is free to translate array dereferences of the form a to the form *(a+b), thus, because addition is commutative, the following is perfectly valid code:

    int a[3];
    int i;

    i=0;
    i[a] = 0[a] = 3;
    (++i)[a+1] = 4;
     
    tshuck likes this.
  12. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    This is why you can't do pinter arithmetic with void pointers

    1. Why would you want to? Without a typecast the compiler can not know or assume what this pointer is about.

    The standard things work in C in a well understood way but if you want to do something obscure, often you have to use many brackets to control the order, and you'll need typecasting as well.

    There might be a way to define the behaviour in a regular way but it might be akward and even more obscure.

    At first, you need to understand perfectly micro step for micro step what you want to do, what is really going to happen, and why.

    Microstep is somehow similar to assembler- an atomic action that can not be split up or divided up further.

    Over the years some people would understand a little about the internals of a C compiler. The MSDN documentation is quite good even if not extensive that much- it mentions the correct technical terms.

    If you spell a complex C line, it will result in quite a lot atomic operations. They will of course happen in some order, and depending on the nature of the spell, different order may give a different result (or not).

    If often helps to break up C expressions into multiple lines to find a mistake.
     
  13. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    That was the point. WBahn was saying that an increment on a pointer will change the object pointer to point at the correct location for the(possible) next object of that type because the pointer type is known...

    As far as the rest of that....why?:confused:
     
  14. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    I always consider threads like this one as interesting.

    How little did I understand when I started to use interpreted BASIC.
    How little terminology did I know. Next to nothing.
    How primitive my programs were.

    So if you look the MSDN reference, it can not really be said all this would be obvious to almost anyone.
     
  15. WBahn

    Moderator

    Mar 31, 2012
    17,775
    4,804
    You shouldn't, that's the point. But many people (myself included, at one point) assume that a void pointer is just a pointer that can point to anything -- sort of some kind of catch all generic pointer. It is not unreasonable, then, to further assume that, being the most basic general catchall type of pointer, that a void pointer simply works directly with byte addresses. Many people encounter void pointers about the same time they encounter sizeof() and learn that the sizeof(int) is not the same on all machines. So it is easy to assume that a void pointer is an implementation independent pointer that you can always count on the behavior of. It's only after you understand pointers and how to use them quite a bit more thoroughly that you understand what a void pointer is and what it is not.
     
Loading...