Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8722325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:23:18+00:00 2026-06-13T07:23:18+00:00

I was trying something else but suddenly stuck with this infinite loop . Please

  • 0

I was trying something else but suddenly stuck with this infinite loop .
Please suggest an answer with explanation that what’s going on here in the below for loop

#include<stdio.h>

int main()
{
 int x=0;
 int i;
 int array[5];
 for(i=0;i<=5;i++)
 {
  array[i]=x;
  printf("#%d value set in index %d\n",x,i);
 }

 return 0;
}

When I remove = sign in the condition of for loop It works fine.

But when I put this it goes to infinite loop , Why?
Accessing extra element (more than its limit) in array is undefined behaviour or what ?
Any help will appreciated.
Thanks in advance.

~

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T07:23:19+00:00Added an answer on June 13, 2026 at 7:23 am

    To avoid causing errors like this as easily, here’s two good rules for writing for loops over actual (local) arrays:

    1. Iteration starts at index 0, since C’s arrays are 0-based.
    2. Use <, always. Never <=.
    3. Don’t repeat the size, let the compiler compute it using sizeof array / sizeof *array. Note the asterisk in the second term.

    So, this loop should have been written:

    for(i = 0; i < sizeof array / sizeof *array; i++)
    

    and then you would have been safe.

    Note that this only works for “real” arrays that have a size visible to sizeof, if you’ve let the array “collapse” into a pointer it won’t work.

    Also note that sizeof is not a function, so no ()s are necessary around its argument in cases like these.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was trying to do something else but kept on removing code thinking that
I'm trying to do a replace on a string like this: $('#example_id').replace(/abc123/g,'something else') But
I'm trying to do something like this in C++: if(){ int a; } else
I'm trying something like this, but this example does not work. jsObj = {};
I'm trying something new, I would normally do this in C# or VB. But
I'm trying to match a URL something like http://example.com/this.is.my.full.hostname/something/else apparently when I pass the
I'm trying to use Graphviz dot (but am willing to use something else) to
Trying to pass something to something else. Says undefined. Not sure how or why.
I am trying to open excel 2003 workbook and save it as something else,
Hello I'm trying something .. I was reading Jquery traversing documentations some forums, this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.