c - For Loop condition -


#include <stdio.h> #define all_mem (sizeof(x) /sizeof(x[0])) int x[] ={1,2,3,4,5,6,7,8,9,10};       int main(void) {             //  printf("the condition %d",(all_mem-2));             int i;                 for(i=-1;i<=(all_mem-2);i++)             {                 printf("the number %d",i);             }             return 0;         } 

in above code loop not executing single time, tried printing condition , satisfies loop condition. insights how macro expression in loop condition evaluated value less -1?

the all_mem macro returning size_t value; integer promotion rules mean comparison of i <= (all_mem - 2) promoting i size_t, means value huge, rather -1. try casting ensure signed comparison:

for(i = -1; <=(ssize_t)(all_mem- 2); ++i) 

Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -