[Courses] [C] Chapter 4d
Laura Bowser
lbowser at andrew.cmu.edu
Fri Oct 11 17:26:20 EST 2002
In this case, ++x and x++ are the same.
They differ when you use them for things like arrays
int array[5] = {1,2,3,4,5};
int x = 0;
printf("++x = %d\n",array[++x]); /* prints 2 */
but, if instead you did
printf("x++ = %d\n", array[x++]); /* prints 1 */
in the first case, X was incremented *before* it was used, so array[1] would
be printed. In the second case, x was incremented *after* it was used, so
array[0] would be printed.
Laura
> ------------------Start program--------------------
> #include <stdio.h>
>
> int main(void) {
> int x = 1;
>
> printf("%d\n", x);
>
> x++;
> printf(%d\n", x);
>
> x = 1;
> ++x;
> printf("%d\n", x);
> }
> -----------End Program-------------
>
> The results:
> 1
> 2
> 2
>
> So, x++ and ++x are the same.
>
> _______________________________________________
> Courses mailing list
> Courses at linuxchix.org
> http://mailman.linuxchix.org/mailman/listinfo/courses
--
Public Key available at:
http://www.elwing.org/~elwing/lbowser.gpg
More information about the Courses
mailing list