3

While I was watching the walkthrough for pset2 (capitalize), I noticed that David didn't declare a variable in his program. However, it still worked and I was wondering why.

I tried to implement my own version of the program. Here's my code

#include <stdio.h>
#include <cs50.h>
#include <string.h>

int main(void)
{
    printf("Please enter in data: ");
    string s = GetString();

    for(int i = 0; n = strlen(s); i < n; i++)
    {
        if(s[i] >= 'a' && s[i] <= 'z')
            {
                printf("%c", s[i]-('a' - 'A'));
            }
    }
        printf("\n"); 
}

but it seems that it would only work if I declared the variable n outside of the for loop. But not if you declare n within the for loop.

So, my main question is did I miss something he typed?

I thought you had to declare all the variables, which he doesn't do. Even if I declare int n = strlen(s); within the loop, it won't compile.

8

Variables can be declared and initialized in different ways.

Variable Declaration

There are two common methods for declaring variables. Suppose we want to declare two int variables, x and y

Method 1:

int x; // declare x
int y; // declare y

Method 2

int x, y; // declare x and y

Notice that method 2 works only if x and y (and probably more if we want) are of the same type (i.e., int in this case).

Variable Declaration and Initialization

Method 1:

int x = 10; // declare and initialize x
int y = 20; // declare and initialize y

Method 2:

int x = 10, y = 20; // declare and initialize x and y

Method 3:

int x; // declare x
int y; // declare y
x = 10; // initialize x
y = 20;

Method 4:

int x, y; // declare x and y
x = 10; // initialize x
y = 20; // initialize y

Notice that using the second method, as well as the first method, we may (or may not) initialize the variables we want. For example, if I don't want to initialize y right I can simply write something like this:

int x = 10, y;

What the professor does in the video, basically, is that he declares two variables (i and n) of type int and initializes them on the same line (see Declaration and Initialization - Method 2). This is definitely a good design tip because after the for loop ends execution both variablesi and n go out of scope (i.e., they're no longer exist). And that's good because we don't really them after the for loop anymore.

so the following code does work.

for (int i = 0, n = strlen(s); i < n; i++)
{
    // do something
}

The problem with your code is that you put a semicolon (i.e., ;) instead of a comma (i.e., ,) when you separated the two variables i and n. And since the for loop declaration expects only three sections separated with two semicolons between them as follows

for (declaration and initialization; continuation condition; update)
{
    // do something
}

It was confused because you provided 3 semicolons!

| improve this answer | |
  • Yes, my bad, it's ok to declare 2 variables of the same data type just using a coma as a separator. – supeindesu Jun 16 '14 at 18:33
  • Ah, yes. I totally see that now. These are the small things that I know, but don't really KNOW, if you know what I mean. – Jonobugs Jun 16 '14 at 23:43
1

You are declaring a variable n inside the 'for' loop command not assigning it a data type, also, you are using ; when you need to use , in that case:

for(int i = 0, n = strlen(s); i < n; i++)

Edit: my mistake, the problem it's just in the ";"

| improve this answer | |
  • Yes, exactly! This is what David wrote, but when he compiled it, it worked. Also, I tried doing it exactly the way you wrote, but it still came up with errors. The only way I was able to get it to work was to declare it BEFORE the FOR loop. – Jonobugs Jun 16 '14 at 16:38
  • But in the version you posted there's the problem with the ; before the variable declaration, that will always throw an error. – supeindesu Jun 16 '14 at 17:00

You must log in to answer this question.

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