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 8282457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:22:35+00:00 2026-06-08T10:22:35+00:00

Consider a loop in C which declares a character array in the loop’s body.

  • 0

Consider a loop in C which declares a character array in the loop’s body. At each iteration, a character of array is modified until the end is reached. At the end, the variable is printed. The description would expand to the next code:

#include <stdio.h>

int main(void) {
    int i = 0;
    for (;;) {/* same as: while(1) { */
        char x[5];
        x[i] = '0' + i;
        if (++i == 4) {
            x[i] = '\0'; /* terminate string with null byte */
            printf("%s\n", x);
            break;
        }
    }
    return 0;

Many may expect 0123 as output. But for some reason GCC 4.7 does not do that when compiling with optimization enabled (-O1 and higher). It instead puts random data in the first bytes of the character array, which becomes:

| 0  |  1  |  2  |  3  |  4   |
|     RANDOM     | '3' | '\0' |

I think that this is logical behaviour from the language point of view: automatic variables are gone after a block is terminated, so the above “random” behaviour should be expected.

What should be the correct behaviour? I know that moving the declaration of x outside the loop “fixes” it, but that does not say anything about the behaviour of this snippet. The real-world problem is a bug in Netfilter.

  • 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-08T10:22:37+00:00Added an answer on June 8, 2026 at 10:22 am

    Since the array is declared inside the scope of loop’s body, you can think of it as of a new array being allocated in the automatic storage area for each loop iteration. The content of that unititialized array is undefined, except for the character at the index to which you have assigned during the current iteration, so what you see there is indeterminate value:

    C99 standard, section 6.7.8: If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate

    When optimization is turned off, the array lands in the same spot in the automatic storage, so your program gets lucky; however, this is by no means guaranteed.

    Move the array to outside the loop, like this:

    int i = 0;
    char x[5];
    for (;;) {
        x[i] = '0' + i;
        if (++i == 4) {
            x[i] = '\0'; /* terminate string with null byte */
            printf("%s\n", x);
            break;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following sample program in which a loop is running; int main() {
Consider there are three array list each of equal length and having positive, negative
Consider this line in the pygame loop: pygame.display.set_mode().fill((0, 200, 255)) From: http://openbookproject.net/thinkcs/python/english3e/pygame.html I. How
I'm using a jQuery .each() call, which uses an anonymous function, from inside a
Possible Duplicate: Difference between declaring variables before or in loop? Consider the two codes
Consider the following hypothetical people management system. Suppose each Person object belong to a
I'm iterating over a 3 dimensional array (which is an image with 3 values
I've been given the following problem about loop invariants: Consider x=4; for(i=5;i<k;i++) { a=a+x+i;
I have a method which should consider a collection of instances of a class
I have some data from database coming out in a loop. Each of these

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.