the ++ operator

Manolis Stamatogiannakis mstamat at csd.uoc.gr
Wed Nov 19 23:47:47 EET 2003


Tom Diamond wrote:
> Ρε παιδιά, τρέχω το παρακάτω:
> --- begin ---
> int main()
> {
>   float a[3], b[3];
>   float c;
>   int i;
> 
>   /*random numbers*/
>   a[0]=0.1; a[1]=2.54; a[2]=7.12;
>   b[0]=4.1; b[1]=5.21; b[2]=4.12;
> 
>   c = 0.0; i = 0;
>   c += a[i]+b[i]+a[i+1]+b[i+1];
>   printf("%f\n", c);
> 
>   c = 0.0; i = 0;
>   c += a[i]+b[i++] + a[i]+b[i];
>   printf("%f\n", c);
> 
>   c = 0.0; i = 0;
>   c += a[i]+b[i++];
>   c += a[i]+b[i];
>   printf("%f\n", c);
> }
> --- end ---
> 
> Τα τρία αποτελέσματα που τυπώνω δε θα έπρεπε να βγαίνουν ίδια; Το μεσαίο 
> μου βγαίνει διαφορετικό. Τι παίζει με τον '++'; Μυρίζομαι ότι κάτι μου 
> έχει ξεφύγει στην όλη ιστορία αλλά τι;
> 
> Tom.
> 


Dokimase auto, mpas kai sou ly8ei h aporia:
#include<stdio.h>

int main()
{
   int i;

   i=0;
   printf("%d %d %d %d\n",i,i,i+1,i+1);
   i=0;
   printf("%d %d %d %d\n",i,i++,i,i);
   i=0;
   printf("%d %d ",i,i++);
   printf("%d %d\n",i,i);

   return 0;
}


Epishs, h kalyterh prolhpsh gia thn falakra stous C programmers
einai to "gcc -Wall". An kaneis compile px auto pou sou esteila
(h' to diko sou) me -Wall, 8a eixes:

mstamat at masistas mstamat $ gcc -Wall foo.c
foo.c: In function `main':
foo.c:10: warning: operation on `i' may be undefined
foo.c:10: warning: operation on `i' may be undefined
foo.c:10: warning: operation on `i' may be undefined
foo.c:12: warning: operation on `i' may be undefined
mstamat at masistas mstamat $


M.




More information about the Linux-greek-users mailing list