0

I'm just trying to do a simple test run of executing a program and passing it two parameters.

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

int main(int argc, char* argv[])
{
    start = atoi(argv[1]);
    stop = atoi(argv[2]);
    for(start; start < stop; start++)
    {
        printf("hello");
    }
}

Why is this not working? When I compile it says start and stop is not declared. And when I try to run it as a executable like this ./file 3 5 it says "." is not recognized as an internal or external command. I'm using mingw on my widows cmd prompt. Any ideas why it wont run like I want? That being: compiling, entering executable ./file 3 5 then it running like normal?

2
  • 2
    Because you haven't given start or stop types. They need to be int start and int stop the first time you assign to them. Commented Mar 18, 2014 at 1:35
  • 1
    Do you know what variable declarations are? Because you don't have any. Commented Mar 18, 2014 at 1:35

4 Answers 4

3

That's because you didn't declare start or stop. You have to declare variables before you can use them. They don't just show up out of thin air.

Sign up to request clarification or add additional context in comments.

Comments

2

try this

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

int main(int argc, char* argv[])
{
    int start = atoi(argv[1]);
    int stop = atoi(argv[3]);
    for(start; start < stop; start++)
    {
        printf("hello");
    }
}

1 Comment

argv[2], typo, sorry my mobile is bugging out.
2

The windows shell you're using is a basically DOS-ish thing. ./prog doesn't work because the slash is not a directory separator. You have to use .\prog, or just prog because . is always implicitly in the PATH.

This is in addition to your lack of declaration of start and stop which is well covered by the other answers and comments.

Comments

1

Well, you are actually using start and stop variables without declaring them.

You are passing to the main two strings (argv[1] and argv[2]) that are supposed to be integers; as a consequence you need your start and stop variables to be integers as well.

Therefore you have two options (which are the same, just pick your favourite one):

int start = atoi(argv[1]);
int stop = atoi(argv[2]);

or

int start, stop;
start = atoi(argv[1]);
stop = atoi(argv[2]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.