0

I want to iterate through all possible combinations of the alphabet, including single letter outputs. For this purpose I included the use of '\0' so the first print out in the iteration is the sigle letter without a second letter after.

When I print it out, it all works well for a while but at some point it starts to skip '\0' every two loops, with the result that it alternately skips printing half of the single uppercase letters. I have no idea why it acts this way...

This is an extract of the result I get (this part is correct, notice single letters 'g', 'h', 'i' all get printed):

fZ g ga gb gc gd ge gf gg gh gi gj gk gl gm gn go gp gq gr gs gt gu gv gw gx gy gz gA gB gC gD gE gF gG gH gI gJ gK gL gM gN gO gP gQ gR gS gT gU gV gW gX gY gZ h ha hb hc hd he hf hg hh hi hj hk hl hm hn ho hp hq hr hs ht hu hv hw hx hy hz hA hB hC hD hE hF hG hH hI hJ hK hL hM hN hO hP hQ hR hS hT hU hV hW hX hY hZ i ia ib ic

But after a while it starts skipping letters (it prints 'W' and 'Y' but not 'X'):

VZ W Wa Wb Wc Wd We Wf Wg Wh Wi Wj Wk Wl Wm Wn Wo Wp Wq Wr Ws Wt Wu Wv Ww Wx Wy Wz WA WB WC WD WE WF WG WH WI WJ WK WL WM WN WO WP WQ WR WS WT WU WV WW WX WY WZ Xa Xb Xc Xd Xe Xf Xg Xh Xi Xj Xk Xl Xm Xn Xo Xp Xq Xr Xs Xt Xu Xv Xw Xx Xy Xz XA XB XC XD XE XF XG XH XI XJ XK XL XM XN XO XP XQ XR XS XT XU XV XW XX XY XZ Y Ya

This is the original code:

int main(void)
{
    char abc[52] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
    char xyz[3];
    char null = '\0';

    for (int i = 0; i < 52; i++)
    {
        xyz[0] = abc[i];
        xyz[1] = null;
        printf("%s  ", xyz);
        for (int j = 0; j < 52; j++)
        {
            xyz[1] = abc[j];
            printf("%s  ", xyz);
        }
    }

    printf("\n");    
}
2

This is a textbook example of "undefined behaviour". It gives different results on different machines because, well, there's a bug and there is no way to predict when or how it will "work" or fail.

Inside the j loop, xyz is not properly terminated as a string.

To be clear, it is not an issue with the sandbox. It is a garden variety bug.

0

Ok, I just started week 3 and tried the nre IDE and it seems to be working perfectly fine... so apparently it was an issue with the sandbox!

I leave this here in case someone else stumbles upon a similar issue or want to look into it.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .