0

For some reason that I can't comprehend ,this program keeps looping when I execute it in the CMD.

#include <stdio.h>
int main() {
    char array [] = {'b','f','r','o','a','u','v','t','o','\0'};
    int grootteArray = sizeof(array);
    int grootteChar = sizeof(char);               
    int lengteArray = grootteArray / grootteChar;
    int i;
    for (i = 0; i < lengteArray + 1; i + 2) {
        printf("%c", array[i]);
    }
    return 0; 
}
4
  • In addition to all these answers, be careful because sizeof(array) will always be 4. To get the length of the string use strlen() from string.h. Commented Oct 13, 2013 at 12:07
  • 2
    @Sebastian-LaurenţiuPlesciuc That is not correct.sizeof(array)== 10 ideone.com/fork/agvGKv Commented Oct 13, 2013 at 12:10
  • @Sebastian-LaurenţiuPlesciuc, you really should read about arrays and pointers, they are not the same in C. Commented Oct 13, 2013 at 12:34
  • Whoops my bad. Thought it was sizeof(char *). Commented Oct 13, 2013 at 14:40

4 Answers 4

8

Your counter variable remains the same after each loop. You need to increase it by assigning the new value:

for (i=0; i<lengteArray+1; i=i+2) //change to this
Sign up to request clarification or add additional context in comments.

Comments

7

Because i never changes. You should assign i + 2 to i:

for (i=0; i<lengteArray+1; i = i + 2) {
                             ↑

The way you wrote it has no effect on the value of i, it just calculates i + 2 and does nothing with this value.

You're getting infinite loop because i is set to 0 and it's always 0.. So once i < legteArray + 1, it'll remain like that.

5 Comments

Doesnt the i+2 count?
@BURNS:- No that is not the syntxx for the for loop!
What do you mean by count?
i + 2 never changes the original variable. It just computes the result and discards it.
Thank you :) I meant it as in counting up ,i+2 for each loop.
2

The value of i is never changed.

try this:-

for (i=0; i<lengteArray+1; i = i+2) {

Comments

2

i is not modifying in you loop. Also i < lengteArray + 1 will lead you to print garbage value and undefined behavior.

for (i = 0; i < lengteArray; i++){
    printf("%c", array[i]);
}     

If you are interested in incrementing i by 2 in each iteration then you can use i += 2

for (i = 0; i < lengteArray; i += 2){
    printf("%c", array[i]);
}    

15 Comments

@ComFreek; Actually some people on SO really likes me. Seriously!
@Kunal:- Even if the original answer was wrong but now since the answer is now changed and is correct you may take down your downvote. Lets try to keep the spirit of SO at its best :)
@ComFreek I would take back my downvote happily if I had given one. Replying to your query was a bad decision :)
@Kunal; I never said you downvoted or remove your downvote. I am expecting an explanation for your previous comment.
@haccks I know that from my own experience here (not that Kunal is one). Oh, serial downvoting also happened once to me. Report it to [email protected] and all reputations will be restored.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.