Code:#include<stdio.h> #defineTOTAL_ELEMENTS (sizeof(array) /sizeof(array[0]))intarray[] = {23,34,12,17,204,99,16}; intmain() { intd; for(d=-1;d<= (TOTAL_ELEMENTS-2);d++) printf("%d\n",array[d+1]); return0; }
Code:#include<stdio.h> #defineTOTAL_ELEMENTS (sizeof(array) /sizeof(array[0]))intarray[] = {23,34,12,17,204,99,16}; intmain() { intd; for(d=-1;d<= (TOTAL_ELEMENTS-2);d++) printf("%d\n",array[d+1]); return0; }
You are using an identifier before declaring it.
Identifiers must be first declared before using them in a function.
According to my compiler:Originally Posted by Swoorup
Code:test.c:2:2: invalid preprocessing directive #defineTOTAL_ELEMENTS test.c:4: warning: return type defaults to `int' test.c: In function `intmain': test.c:5: error: `intd' undeclared (first use in this function) test.c:5: error: (Each undeclared identifier is reported only once test.c:5: error: for each function it appears in.) test.c:6: error: `d' undeclared (first use in this function) test.c:6: error: `TOTAL_ELEMENTS' undeclared (first use in this function) test.c:7: error: `array' undeclared (first use in this function) test.c:8: error: `return0' undeclared (first use in this function)
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Sorry the code turned awkward when copying and pasting it.
This is the one:
Still does not work thoughCode:#include<stdio.h> #define TOTAL_ELEMENTS (sizeof(array) /sizeof(array[0])) int array[] = {23,34,12,17,204,99,16}; int main() { int d; for(d=-1;d <= (TOTAL_ELEMENTS-2);d++) printf("%d\n",array[d+1]); return 0; }
read the above post.
No, it works though if this line
for(d=-1;d <= (TOTAL_ELEMENTS-2);d++)
is replace by for(d=0;d <= (TOTAL_ELEMENTS-2);d++)
I just don't understand why d=-1 does not make the whole program loop and print the elements
Dude, you won't get any output even if you change d=-1 to d=0.
You don't believe me at all
if d=0
this is the output:
34
12
17
204
99
16
Press any key to continue...
And with d=-1
Press any key to continue...
Please do see for yourself. I had found the code somewhere but forgot where! It was somewhere from a c puzzle like site. But today I just got to stumble across it
> I just don't understand why d=-1 does not make the whole program loop and print the elements
Because the comparison is int <= size_t
But since size_t is an unsigned number, the int gets promoted to unsigned as well, resulting in a comparison of
if ( 0xffffffff <= 6 )
which is obviously false, and therefore nothing happens.
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.
So the -1 gets converted to this 4294967295?
That's really interesting but confusing at the same point.
Thank you sir.
What I believe is that you had a problem and rather than think it over a bit and fix it, you started throwing code at it to see what stuck...
Code:#include<stdio.h> #define TOTAL_ELEMENTS (sizeof(array) /sizeof(array[0])) int array[] = {23,34,12,17,204,99,16}; int main() { int d; for(d = 0; d < TOTAL_ELEMENTS; d++) printf("%d\n", array[d]); return 0; }