3

this is my code which I expect to see

7, 3, 5, 1, 9

as output but it prints

0, 7, 3, 5, 1

#include <stdio.h>
#define LEN 5

int main(int argc, char const *argv[])
{
    int arr[LEN];
    int index;
    arr[0] = 7;
    arr[1] = 3;
    arr[2] = 5;
    arr[3] = 1;
    arr[4] = 9;

    int ITM;
    for(int IDX = 0; IDX < sizeof(arr) / sizeof(int); ITM = arr[IDX++]){
        printf("%d, ", ITM);    
    }

    return 0;
}

what is wrong with this code?

BTW, I know that I can move "ITM = arr[IDX++]" into loop body!

8
  • 1
    So... what do you know about the for statement? Commented Oct 5, 2016 at 8:07
  • 2
    I agree - there is nothing wrong with this question. It's well-written, has a code snippet, and expected and actual output. What could be better? Please do not downvote on grounds of obviousness. Commented Oct 5, 2016 at 8:13
  • 2
    @Bathsheba, Ashish, sepisoad - In a community based system, the community controls the content, and in the SO community that means voting. The voting system is to be used at the discretion of us all, and we all have the right (obligation if you ask me) to use it how we see fit. As for the closing reason - I think minimal effort is a prerequisite for posting a question. This was clearly not demonstrated in this question. and is what I expressed in my close reason. I don't see any of that as being "not nice", and the one comment that does use foul language (sepisoad's) is the one to criticize. Commented Oct 5, 2016 at 8:27
  • 1
    @Amit: we do ask you to come up with a valid off-topic reason; not enough research is not one of those, that's what the downvote option is for. Commented Oct 5, 2016 at 10:00
  • 1
    @Amit: again, quality is not an off-topic reason. Low quality can be improved on by editing. The information you are linking to prevents posts that don't meet a minimum bar from being posted, but that doesn't mean that those posts would otherwise be off-topic. Commented Oct 5, 2016 at 10:22

3 Answers 3

3

The behaviour of your program is undefined due to your reading the unitialised variable ITM on the first iteration of the for loop.

ITM is not set to anything until ITM = arr[IDX++] is evaluated, and that doesn't happen until program control gets to the closing } of the for loop.

Writing flashy code like this is occasionally useful, but you need to be aware of the pitfalls.

Sign up to request clarification or add additional context in comments.

1 Comment

yup, thanks, BTW I wanted to write a foreach macro using this loop, is there a workaround?
3

The last statement of the for loop is executed after the first iteration (or IDX would be 1 at once)

So at first ITM is just undefined.

EDIT: after providing a potentially unsafe macro, I removed it from my post. I suppose that if you want to use foreach stuff, switch to C++ 11.

5 Comments

actually I wanted to write a foreach macro using this loop, is there a workaround?
strictly speaking, i=a[++IDX] is UB for the last iteration with IDX == number of elements - 1
Please don't recommend to use such ugly, unsafe macros. This is very bad programming practice. Overall, re-inventing the C language and replacing it with your private, secret macro language is a genuinely bad idea.
FOREACH(my_incorrect_type, arr) Halt and catch fire. FOREACH(ITM, incorrect_array_type) Halt and catch fire. FOREACH(IDX, arr) Halt and catch fire. FOREACH(ITM, pointer) Halt and catch fire. I'm starting to notice a use-case pattern here...
you're both right. It was fun but I'll edit it out because of UB.
0
#define LEN 5

int main(int argc, char const *argv[])
{
    int arr[LEN];
    int index;
    arr[0] = 7;
    arr[1] = 3;
    arr[2] = 5;
    arr[3] = 1;
    arr[4] = 9;

    for(int i = 0; i < sizeof(arr) / sizeof(int); i++){
        printf("%d, ", arr[i]);
    }
    printf("\n");


    return 0;
}

if you want to use foreach in c read that

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.