Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Why does it show that variable 'i' undeclared in loop? (C language)

–8 votes
asked Jun 7 by tearaj (150 points)

#include<stdio.h>

int main()

{

    int a[5][5],b[5][5],r1,c1;

    printf("Enter number of rows and columns: ");

    scanf("%d%d",&r1,&c1);

    printf("Enter the values of the matrix: ");

    

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

    {

        for(int j=0;j<c1;j++)

        scanf("%d",&a[i][j]); 

    }

    

    printf("\nThe matrix is:\n");

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

    {

        for(int j=0;j<c1;j++)

        printf("%d\t"a[i][j]);

        printf("\n");

    }

}

Note that the program isnt fully completed and that the program does work if i declare them outside both the loops. 

Program is written in C language.

13 Answers

0 votes
answered Jun 8 by emm-design (180 points)
In most cases this error  get in when you have not defined the "i" as of a particular datatype.To solve this type in the datatype either within the loop statement or outside.
0 votes
answered Jun 8 by shaswataddas (150 points)

Because you put a semicolon (;) where you start a for loop. (for i and j both)

CHECK : for(int i=0;i<r1;i++);

0 votes
answered Jun 8 by Gor Hakobyan (150 points)

Your for loops end with a semicolon ';'

Remove semicolon, it will work!

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

+4 votes
answered Jun 8 by xDELLx (8,580 points)
why do you have ";" on lines # 17 ,31??

adding ";" after for loop, makes it an empty loop & also limits  the scope of i within the empty loop.

removing the ";" on line 17 ,31 will solve the issue .
commented Jun 14 by tearaj (150 points)
thank you.
 it was a typo on my part and i couldn't find it out
0 votes
answered Jun 10 by Akash_2k19_006 (150 points)
#include<stdio.h>

int main()

{

    int a[5][5],b[5][5],r1,c1;

    printf("Enter number of rows and columns: ");

    scanf("%d%d",&r1,&c1);

    printf("Enter the values of the matrix: ");

    
for(int i=0;i<r1;i++)
{
    for(int j=0;j<c1;j++)
    {
        scanf("%d",&a[i][j]);
    }
}

    

    printf("\nThe matrix is:\n");
for(int i=0;i<r1;i++)
{
    for(int j=0;j<c1;j++)
    {
        printf("%d\t",a[i][j]);
    }
    printf("\n");
}
    return 0;

}
+1 vote
answered Jun 10 by VamshiSaini01 (160 points)
don't keep semicolons for (for-loops).
0 votes
answered Jun 11 by svyshnavi12 (150 points)
No semicolons for "FOR LOOP"after for(  ) .
+1 vote
answered Jun 12 by Meenakshi. R (160 points)
There should be no semicolon after the for loop..It means that  the same loop will execute until the condition fails..it won't enter into that..
+1 vote
answered Jun 13 by Albert Lamy (170 points)
In C language, we put the semicolon (;) only at the end of an instruction. However, for() is a loop and not an instruction, which is why for() shouldn't have a semicolon at the end. You just have to remove the semicolons at the end of the declaration of your loops.
+1 vote
answered Jun 15 by VinayTrep (170 points)
Don't declare i multiple times.. declare it in the top.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...