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

  • SEARCH
  • Home
  • 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 3854580
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T17:38:41+00:00 2026-05-19T17:38:41+00:00

I am stuck with this mystery regarding for loop. int abc[3], i, j; for(j=0;

  • 0

I am stuck with this mystery regarding for loop.

    int abc[3], i, j;
    for(j=0; j<3; j++);
    printf("%d\n", j);
    abc[j] = abc[j] + 3;
    printf("%d \n", j);


Output: 

3
6 

Output should have been 3,3 as I’ve not changed value of j.

Adding 3 to the jth value of abc has resulted in change of value of j by 3. This happens only while exiting from a for loop and then trying to change the value of abc[j].

Maybe I am missing something pretty obvious. Any help would be much appreciated.

  • 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-05-19T17:38:41+00:00Added an answer on May 19, 2026 at 5:38 pm

    You have a buffer overflow since you declared your array to have size 3 int abc[3]; yet you are indexing the 4th element; this is Undefined Behavior.

    abc[j] = abc[j] + 3; // j = 3 here, overflow
    

    What you are most likely seeing is that j is located on the stack just past your array abc and so when you overflow one past the array with abc[3], you’re actually modifying the memory that contains j.

    *Note that nowhere in the C standard does it mention the word stack, this is an implementation detail and can change from system to system. This is partly the reason why it is Undefined Behavior and you are getting responses from people that they see two 3’s as output.

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

Sidebar

Related Questions

I have been stuck on this for days, and was wondering if anyone had
I've been stuck with this for weeks now and have no idea where I'm
I have been stuck with this problem for a while now, and I just
I've been stuck on this question for a while. I have a data file
Been stuck on this for a while, I have a .dat file with 16
I have been stuck on this problem for awhile now, and I was hoping
I have been stuck at this for a while. I have a smartgwt widget
I've been stuck on this EXC_BAD_ACCESS error 2 days now. I have a reloadAnnotations
I have been stuck on this problem for almost 2 days now, consider the
I've been stuck on this all day, and have just run out of ideas.

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.