what is the output of this snippet ?

Hello ,

I had written a small C code snippet.
Why the below snippet doesnt print anything.

#include<stdio.h>

#define TOTAL_ELEMENT (sizeof(array) / sizeof(array[0]))
int array[]={1,2,3,4,5,6,7};

int main()
{
int d;
for(d=-1;d<=(TOTAL_ELEMENT-2);d++)
{
printf("%d \n",array[n+1]);
}

return 0;
}
My first programming teacher had a saying. "You're scratching your left ear with your right hand."
This is like scratching your left ear with your right knee.
1. Why does d start at -1?
2. Why is it array indexed with d+1 (although it actually says n+1, which means the program doesn't even compile).
3. Why is the for condition <=?
4. Why "d<=(TOTAL_ELEMENT-2)"?
Last edited on
To answer your question , i got this code snippet for my test.
2) array is indexed with d+1 ( typing mistake above as n+1)
Your question is valid and a tough one but I don't know the answer. I suspect it has to do with C macro substitution gone wrong.

1
2
3
4
5
6
int total = TOTAL_ELEMENT;
for(d=-1;d<=(total-2);d++)
{
printf("%d \n",array[d+1]);
}


Above works. So I suspect when we put d<=(TOTAL_ELEMENT-2) it is not working at all. When I substitute inside d<=((sizeof(array)/sizeof(array[0]))-2) still cannot. Then I remove -2 like d <= (sizeof(array)/sizeof(array[0]) also cannot.

I am suspecting the macro TOTAL_ELEMENT is not evaluated inside the for loop syntax which is strange. Hmmmm.... hope to see expert C++ advice on this problem though.
closed account (D80DSL3A)
No expert here. Just noticed the #define preceeds the declaration of array so the sizeof() functions may give garbage. I'm surprised it will build since array doesn't exist yet where the #define is made. Try reversing these lines.
It is easy to try out and it still prints nothing even with the order is reversed. Please note macro substitution is tricky business.

Take a look at http://en.wikipedia.org/wiki/C_preprocessor

There seem to be 2 kinds of macro. Object-like and function-like. I presume function-like will attempt to execute and get the result whereas object-like just do plain text substitution ?
I tried following code , but it doesnt work .... real surprise and confusion ...

#include<stdio.h>

int array[]={1,2,3,4,5,6,7};

#define TOTAL_ELEMENT (sizeof(array) / sizeof(array[0]))

int main()
{
int d;
for(d=-1;d<=(TOTAL_ELEMENT-2);d++)
{
printf("%d \n",array[d+1]);
}

return 0;
}
The more I try the more I get confused.

for(d=-1;d<=(TOTAL_ELEMENT-2);d++) ...

If you change to d=0 for above it does print out but not when you gave a negative index.

for(d=0;d<=(TOTAL_ELEMENT-2);d++) ...
gcc says:
warning: comparison between signed and unsigned integer expressions

It refers to this -> d<=(TOTAL_ELEMENT-2);

What happens is that d is 'converted' to an unsigned int before the comparison. When you take -1 and see it as an unsigned int, it's quite big... That's why the loop body never executes. This will work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<cstdio>
#define TOTAL_ELEMENT (sizeof(array) / sizeof(array[0]))

int array[]={1,2,3,4,5,6,7};

int main()
{
    int d;

    for(d=-1;d<=signed(TOTAL_ELEMENT-2);d++)
    {
        printf("%d\n",array[d+1]);
    }

    return 0;
}
Last edited on
I narrow it down to sizeof which actually return unsigned int.

In the code, for(d = -1; d <= (TOTAL_ELEMENT-2); d++) the very first comparison fail.

if (-1 <= (TOTAL_ELEMENT-2)) cout << "here?\n";

Above does not print out.

if (0 <= (TOTAL_ELEMENT-2)) cout << "here?\n";

Above print out.

So the comparison against -1 fail but 0 is success. So I go research on sizeof function.

http://en.wikipedia.org/wiki/Sizeof

Above says "A sizeof expression evaluates to an unsigned value equal to the size in bytes of the "argument" datatype, variable, or expression (with datatypes, sizeof evaluates to the size of the datatype; for variables and expressions it evaluates to the size of the type of the variable or expression)."

Now the mystery is solved. m4ster r0shi post is correct. This is tricky question.
thanks to all of you ....
Topic archived. No new replies allowed.