Why does this program show something else even though the code is correct?

Viewed 102
#include <stdio.h>

int main(void)
{
    int a = 4;
    char alpha[26] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    for (int i = 0; i < 6; i++);
    {
        for (int b = 5; b > a; b--)
        {
             printf("%c", alpha[b]);
        }
        a--;
        printf("\n");
        printf("%d\n", a);
    }
    return 0;
}

I want this code to print like this:

F
FE
FED
FEDC
FEDCB
FEDCBA

but the output it currently showing is this:

F
3

I don't know whether there is any typo cause i have checked a lot i thing i have done something else as wrong. if it's a typo just tell me it's because of typo if don't please help me with this as i am new to c programming language.

6 Answers

As others have noted, your outer for loop has a ; at the end, so it has an empty body:

for(int i = 0; i < 6; i++);

Note that you should always compile with warnings enabled (e.g.):

gcc -c -Wall -Wextra -Werror foobar.c

If you had, the compiler would have flagged this statement.


gcc:

foobar.c: In function ‘main’:
foobar.c:7:9: warning: this ‘for’ clause does not guard... [-Wmisleading-indentation]
         for(int i = 0; i < 6; i++);
         ^~~
foobar.c:8:9: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the ‘for’
         {
         ^

clang:

foobar.c:7:35: warning: for loop has empty body [-Wempty-body]
        for(int i = 0; i < 6; i++);
                                  ^
foobar.c:7:35: note: put the semicolon on a separate line to silence this warning
1 warning generated.

Note that newer versions of gcc don't need -Wextra.

clang will flag the statement with just clang -c foobar.c.

And, originally, gcc produced a similar "empty body" warning to clang's

"Why the programme is showing something else even though the code is correct?"

Because you got a semicolon ; at the end of the head of the first for loop:

for(int i = 0; i < 6; i++);

This will cause that the presumed loop body is not treated as loop body. It is treated as a compound statement after the first loop has been completed.

That is why you get this output.


Remove the semicolon and your program will print:

F
3
FE
2
FED
1
FEDC
0
FEDCB
-1
FEDCBA
-2

Furthermore, Remove printf("%d\n", a); to get the expected output of:

F
FE
FED
FEDC
FEDCB
FEDCBA

I also real recommend you to read the clues in Craig´s answer regarding compiler flags and to never ignore compiler warnings as this is a code issue a compiler can, and dependent upon the compiler also by default, warn you about.

To get the expected output remove ; at the end of first loop and remove last printf

for(int i = 0; i < 6; i++) { 
    for(int b = 5; b > a; b--) { 
        printf("%c", alpha[b]); 
     } 
     a--; 
     printf("\n"); 
}

This loop has ; at the end. There is no body. It does nothing.

    for(int i = 0; i < 6; i++);
                              ^

remove the semi-colon at the end of first loop which you placed by mistake

There are a small typo causing a major problem in your code:

  • you have a spurious ; at the end of your outer for statement. The ; is an empty statement, causing the for loop to iterate with an empty body, and its intended body to be executed unconditionally once after the loop.

I would advise to use the Kernighan and Ritchie bracing style that makes this kind of error very unlikely:

#include <stdio.h>

int main(void) {
    int a = 4;
    char alpha[26] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    for (int i = 0; i < 6; i++) {
        for (int b = 5; b > a; b--) {
             printf("%c", alpha[b]);
        }
        a--;
        printf("\n");
        printf("%d\n", a);
    }
    return 0;
}
Related