Like Tree5Likes

pre and post-increment meaning the same

This is a discussion on pre and post-increment meaning the same within the C Programming forums, part of the General Programming Boards category; Hi, I'm wondering whether in that code, Code: void strcat(char s[], char t[]) { int i,j; i = j = ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    39

    Lightbulb pre and post-increment meaning the same

    Hi, I'm wondering whether in that code,

    Code:
    void strcat(char s[], char t[])
    {
        int i,j;
    
    i = j = 0;
    while (s[i] != '\0')
        i++;
    while ((s[i++] = t[j++] != '\0')
        ;
    }
    there would be a difference if I change i++ to ++i in the first while loop. This is a simplified strcat function from K&R (chapter 2.8)

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    No change if postfix or prefix here.Postfix and prefix play a significant role,when they are used to expressions,or in arguments,not when the are 'alone'.You could run your program with postfix and prefix and see that there is no differrence.Is that understood?
    Pole likes this.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    39
    Thanks, understood. But I wasn't able to run this code because function doesn't return anything so no result (which I could put in printf) would be shown.

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I want you to have this examples though .Try to figure out what will happen(what will the output be) before running your code,in order to test yourself

    Test 1
    Code:
    #include<stdio.h>
    
    int main(void) {
        int i = 3;
        i++;
    
        printf("i= %d\n", i);
        ++i;
        printf("i= %d\n", i);
        printf("i= %d\n", ++i);
        printf("i= %d\n", i++);
        printf("i= %d\n", i);
        return 0;
    }
    Test 2
    Code:
    #include<stdio.h>
    
    void print(int var);
    
    int main(void) {
        int i = 3;
        
        print(i);
        
        print(i++);
        
        print(i);
        
        print(++i);
        
        print(i);
        
        return 0;
    }
    
    void print(int var)
    {
        printf("i= %d\n",var);
    }
    If needed post back

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    > Thanks, understood. But I wasn't able to run this code because function doesn't return anything so no result (which I could put in printf) would be shown.
    It's supposed to append t onto s, so if you did

    Code:
    char s[100] = "hello ";
    char t[] = "world";
    strcat( s, t );
    printf("Result=%s\n", s );
    With the regular strcat, what do you see?

    Now with your strcat, what do you see?
    With your strcat doing ++i instead of i++, what do you see?
    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.

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by Pole View Post
    Code:
    void strcat(char s[], char t[])
    {
        int i,j;
    
    i = j = 0;
    while (s[i] != '\0')
        i++;
    while ((s[i++] = t[j++] != '\0')
        ;
    }
    This is a simplified strcat function from K&R (chapter 2.8)
    I could not compile that strcat(even though i checked myself the book,and it had as Pole posted it.So i think that the equivalent is this(correct me if i am wrong.please!
    Code:
    #include<stdio.h>
    
    
    void strCat(char s[], char t[])
    {
        int i,j;
     
    i = j = 0;
    while (s[i] != '\0')
        i++;
    while (t[j] != '\0')
        s[i++] = t[j++];
    }
    
    
    int main(void) {
        char s[100] = "hello ";
    char t[] = "world";
    strCat( s, t );
    printf("Result=%s\n", s );
        return 0;
    }

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,987
    Quote Originally Posted by std10093
    I could not compile that strcat(even though i checked myself the book,and it had as Pole posted it.
    There is a typographical error, and in fact you made a similiar error in the sentence that I quoted above. Basically, you wrote an opening parenthesis, but failed to write a matching closing parenthesis. Likewise:
    Code:
    while ((s[i++] = t[j++] != '\0')
        ;
    There are two opening parentheses, but only one closing parenthesis. It should have been:
    Code:
    while ((s[i++] = t[j++]) != '\0')
        ;
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I was misleaded by this first post.Now compiles just fine.Thanks

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I remember an old post about prefix and postfix operators.The question was what the value of y
    if x=8
    y=x++ - --x + ++x - x--

    This is ambiguous and it depends on the compiler.

    Why do i post this?Because such lines of code must be avoided!

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,987
    Quote Originally Posted by std10093
    No change if postfix or prefix here.
    By the way, this is factually incorrect for the corrected version of the code in post #1.

    Quote Originally Posted by std10093
    This is ambiguous and it depends on the compiler.
    Yes, but more than that: it results in undefined behaviour.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by laserlight View Post
    By the way, this is factually incorrect for the corrected version of the code in post #1.
    Can you be a little more explanatory please?

    Quote Originally Posted by laserlight View Post
    Yes, but more than that: it results in undefined behaviour.
    Yes,that's the point .

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,987
    Quote Originally Posted by std10093
    Can you be a little more explanatory please?
    Simple: with pre-increment, you skip the very first character to be copied.

    Quote Originally Posted by std10093
    Yes,that's the point .
    "ambiguous and it depends on the compiler" sounds like implementation defined behaviour, i.e., there are a few ways that the expression can be evaluated, and which one is chosen depends on the compiler and various other variable. Undefined behaviour is a little different in that, theoretically, upon encountering that expression, a standard conforming compiler could set things up such that the program launches a nuclear weapon (assuming that you have the necessary hardware).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by laserlight View Post
    Simple: with pre-increment, you skip the very first character to be copied.
    Here is the code.I run it with pre and post increment.I can't see the difference.Moreover i can not see the difference in the code
    Code:
    #include <stdio.h>
    
    void strccat(char s[], char t[])
    {
        int i,j;
     
    i = j = 0;
    while (s[i] != '\0')
        i++;
    while ((s[i++] = t[j++]) != '\0')
        ;
    }
    
    int main(void)
    {
        char s[100] = "hello ";
    char t[] = "world";
    strccat( s, t );
    printf("Result=%s\n", s );
    return 0;
    }
    Quote Originally Posted by laserlight View Post

    "ambiguous and it depends on the compiler" sounds like implementation defined behaviour, i.e., there are a few ways that the expression can be evaluated, and which one is chosen depends on the compiler and various other variable. Undefined behaviour is a little different in that, theoretically, upon encountering that expression, a standard conforming compiler could set things up such that the program launches a nuclear weapon (assuming that you have the necessary hardware).
    got it

  14. #14
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,744
    If you do postfix increment on a integer variable, you get the value before the increment as a result, before the next sequence point.
    So x = y++; makes x equal to y - 1.
    Do prefix increment on an integer variable again, and you get the value after the increment as a result.
    So a = ++b makes a equal to b for all b.

    It's a pretty fascinating difference and how you ended up changing the definition of strccat and not noticing anything is beyond me.

    I changed all instances of postfix increment and got this:
    Code:
     Result=hello
    Isn't that clearly wrong?

  15. #15
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    The original poster asked if there would be any difference if the i++ in the first while loop were changed to ++i, and the answer to that is no because nothing depends on either the new value or old value of i within that single statement.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Post Increment an Pre Increment operators in c++
    By anil_ in forum C++ Programming
    Replies: 4
    Last Post: 11-12-2011, 07:27 PM
  2. pre & post increment need help
    By zing_foru in forum C Programming
    Replies: 6
    Last Post: 10-09-2009, 12:14 PM
  3. pre/Post Increment
    By ganesh bala in forum C Programming
    Replies: 1
    Last Post: 02-12-2009, 03:17 AM
  4. post vs pre increment
    By xddxogm3 in forum C Programming
    Replies: 13
    Last Post: 03-19-2004, 04:07 PM
  5. Post increment and pre increment help
    By noob2c in forum C++ Programming
    Replies: 5
    Last Post: 08-05-2003, 03:03 AM

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