0

I have a little confusion about declaring integers and specifically as it relates to the pset1 mario assignment.

I declared my n int for the do while loop I used for user input. For some reason, I also had to declare, at the beginning, my i int used within the nested loops. But, I didn't have to declare for the other int used within the for loops. This would include my j & k.

I didnt want to paste code in here as to give away the programming assignment solution but I used i for the outside counting loop. j and k were the nested loops used to print blank spaces to the left and the # to the right.

Can someone give me a clear understanding of why this is? My program works so it is almost even more confusing.

1 Answer 1

1

You probably did declare them but aren't recognizing it. Did you do something like:

int i, j, k;

which declares 3 integers called i j and k?

You always have to declare your variables, so you must have done so somewhere. :)

One thing that may catch you out is "scope" where a variable will no longer exist outside of the scope in which it was declared.

For example:

do
{
    int a = GetInt();

}
while (a < 5);

will not compile, because the integer called a only exists within those curly braces of the do statement.

Instead:

int a;
do
{
    a = GetInt();
}
while (a < 5);

will be fine.

By the same token, using another example:

for (int i = 0, j = 5; i < j; i++)
{    
    printf("i is now %d and j is %d\n", i,j);
}
printf("j:%d now equals i:%d\n", j,i);

will not work, because i and j only exist within the curly braces of that for loop.

Instead, your compiler will complain like this:

file.c:10:43: error: use of undeclared identifier 'j'
printf("j:%d now equals i:%d\n", j,i);
                                      ^
1 error generated.

but if you want to access i and j outside that loop, you can do this:

int i,j;
for (i = 0, j = 5; i < j; i++)
{    
    printf("i is now %d and j is %d\n", i,j);
}
printf("j:%d now equals i:%d\n", j,i);

and your output should be:

i is now 0 and j is 5
i is now 1 and j is 5
i is now 2 and j is 5
i is now 3 and j is 5
i is now 4 and j is 5
j:5 now equals i:5

(more info)

Okay, unused variable... let's go back to my first example, but with one change:

int a = 0;  // add this
do
{
    int a = GetInt();

}
while (a < 5);

Now, when it compiles, you won't see an undeclared variable a in that while line. Instead, you'll see:

file.c:9:9: error: unused variable 'a' [-Werror,-Wunused-variable]
    int a = GetInt();
        ^
1 error generated.

And why is that? You have declared two integers, both called a. One is in the scope of main() (that first one) and one is only in the scope of the do statement. But you never actually use that a in the do statement, because the a in the while statement is the one you first declared.

Check out the short on "Scope" that is found in Week 2 of the course. https://www.youtube.com/watch?v=UC5QAokAupo

7
  • You are correct- I did declare them within the nested loop. BUT, when I remove the beginning i int (outside nested loop and before the do while), it errors on me. So, I put the int i back at the beginning with my n int. I cant figure out why I ended up declaring the i int twice!
    – Brian T
    Commented Jan 9, 2015 at 3:25
  • see my edited reply.
    – curiouskiwi
    Commented Jan 9, 2015 at 3:36
  • great info- thanks! I think I am getting closer to understanding but I when I try to declare j (and my k) outside the nested loop, I get this error:unused variable 'j' Here is my code structure and see if something is wrong: int n,i; do while for (i=1....) { for (int j=1....) { } for (int k=1...) { }
    – Brian T
    Commented Jan 9, 2015 at 3:58
  • see my edit. :)
    – curiouskiwi
    Commented Jan 9, 2015 at 4:05
  • oh, just saw your edit. are you actually doing anything with j in your loop? incrementing it? printing it?
    – curiouskiwi
    Commented Jan 9, 2015 at 4:07

You must log in to answer this question.

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