0

I've just started to learn C and it's going pretty slow...I wanted to write a program that takes in an integer argument and returns it's doubled value (aka take in integer, multiply by 2, and printf that value).

I purposely did not want to use the scanf function. Here's what I have so far and what is not compiling...

#include <stdio.h>

int main(int index)
{
    if (!(index)) {
        printf("No index given");
        return 1;
    }
    a = index*2;
    printf("Mult by 2 %d",a);

    return 0;

}

So basically when the program is executed I want to supply the index integer. So, in cygwin, I would write something like ./a 10 and 10 would be stored into the index variable. Also, I want to program to return "No index given" and exit if no index value was supplied...

Anyone care to help what I'm doing wrong?

EDIT:

This code returns 1 error upon compilation and is based on the help by @James:

#include <stdio.h>

int main(int 1, char index)
{
    int index, a;
    if (!(index)) {
        printf("No index given");
        return 1;
    }
    a = index*2;
    printf("Mult by 2 %d",a);

    return 0;

}

EDIT 2: Consider a simpler program where a value is just taken and echoed back (as shown below)

#include <stdio.h>

int main(int argc, char* argv[])
{
    int index;
    index = argv[1];

    printf("Index is %d, ", index);
    /*if (!(index)) {
        printf("No index given");
        return 1;
    }
    a = index*2;
    printf("Mult by 2 %d",a);*/

    return 0;

}

This program fails to compile...Any ideas?!? Ugh.

EDIT 3: This is the code that finally compiled and works. Thanks all!

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

int main(int argc, char* argv[])
{
    if (argc <= 1) 
    {
        printf("No index given");
        return 1;
    }
    int i;
        i = atoi(argv[1]); // convert string in argv[1] to integer
    int a;
        a = i*2;
    printf("Mult by 2: %d",a);
    return 0;
} 

Thanks! Amit

Amit
  • 7,688
  • 17
  • 53
  • 68
  • Once you get the args sorted out, as hinted by James, you will find that the argument is the string "10", so you will still need to convert this to an int – John La Rooy Jan 06 '11 at 04:43
  • 1
    @Amit: With regard to your edit, `int 1` is not valid. Each parameter is a variable inside of the function. `1` is not a variable, it's a value. Make sure that you have [a good beginner C book](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list). – James McNellis Jan 06 '11 at 04:50
  • 1
    The problem is that `1` can't be the name of a parameter, and you still don't have quite the right signature. Seriously type `int main(int argc, char* argv[])` on that line in your code. – Eric Mickelsen Jan 06 '11 at 04:50
  • change `int main(int 1, char index)` to `int main(int argc, char* argv[])`. The `index` value that you want is in `argv[1]`. – slebetman Jan 06 '11 at 04:51
  • Okay, I've replaced that line with `int main(int argc ...)`, the program compiled! yes! – Amit Jan 06 '11 at 04:52
  • 1
    You may also often see "int main ( int argc, char ** argv )" – IanNorton Apr 16 '11 at 08:34

4 Answers4

7

There are only two guaranteed-to-work prototypes for the main function: one takes no arguments (int main(void)), the other takes two arguments and looks like this:

int main(int argc, char* argv[])

argc is the number of arguments passed to the program, and argv is an array containing the arguments passed to the program.

If you want to pass an argument when you run the program (and not prompt the user for input), you will need to

  • use the form of main taking arguments,
  • check to make sure argc is greater than one (argv[0] is the program name, or should be),
  • convert argv[1] from its string representation to an integer, using strtol, sscanf, or some other library function (avoid atoi: it provides no usable error reporting),
  • then use that integer.
James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • I see, so would I need something like: `int main(int 1, char* argv[index])` ? – Amit Jan 06 '11 at 04:43
  • 2
    @Amit: You'll want to get [a good beginner C book](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list). – James McNellis Jan 06 '11 at 04:45
  • Hm, okay. I'm still a little confused, I've posted the edited code which returns 1 error upon compilation – Amit Jan 06 '11 at 04:46
  • The standard says that the implementation can define other parameters to `main`, so others (like Unix `char **envp`) can be guaranteed if, say, you can guarantee a given compiler. But that's a technicality. +1 – Chris Lutz Jan 06 '11 at 04:47
  • 1
    @Amit: No, you need `int main(int argc, char* argv[])`. Inside you `main` you then use the `argc` variable to find out how many arguments are in the `argv` array and the `argv` array is an array of strings passed as argument to your program from the command line. – slebetman Jan 06 '11 at 04:49
  • 1
    @Amit: `argc` isn't something you replace with the number of arguments you *want* to get; it's a variable that will contain the number of arguments you *actually* got (counting from the name of the program itself, which counts as the "first argument"). – caf Jan 06 '11 at 05:01
  • @Amit note that the variable names `argc` and `argv` are just that, variable names and thus can be changed to whatever you want. `int main(char foo, char* bar[])` is just as legal but using `argc` and `argv` are accepted conventions. – SiegeX Jan 06 '11 at 05:04
  • This is a much better answer than the current one. – Stephen Melvin Jan 06 '11 at 05:11
1

As you probably know, main() is a "special" function, which expects either 0 or two arguments: int argc, and char **argv, where argc is automagically assigned the number of arguments in the argument array argv. So, whatever arguments you pass to main() will be stored in argv, and that is from where you need to access your arguments.

This link should help.

1

The arguments you are passing into the program are text, and so main will receive them as strings. It splits the command line arguments by whitespace and passes them in as an array of strings, along with a number stating the number of parameters it is giving you. The program will always have at least one argument, the name of the file you ran the program as (which is in this case "a"). This string is always found at argv[0].

As the other answers stated, the correct signature for main is int main(int argc, char* argv[]). When you run ./a 10, argc will be 2 and argv[1] will be the string "10". You will need to convert this string to an integer to multiply it by 2, using something like int i = atoi(argv[1]); or int i; sscanf(argv[1], "%d", &i);.

Null Set
  • 5,374
  • 24
  • 37
0

Here is a correction to your code using the proper prototype for a Main with command line arguments.

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

int main(int argc, char* argv[])
{
     if (argc <= 1) 
     {
         printf("No index given");
         return 1;
     }
     int i = atoi(argv[1]); // convert string in argv[1] to integer
     int a = i*2;
     printf("Mult by 2 %d",a);
     return 0;
} 
Derrick
  • 2,502
  • 2
  • 24
  • 34
  • 1
    `argv[0]` is the name of the executable, not the first argument (and I'm pretty sure you need stdlib.h for `atoi()`) – Wooble Jan 06 '11 at 04:55
  • 1
    `atoi()` has been deprecated in favor of `strtol()` – SiegeX Jan 06 '11 at 05:01
  • By the way, I changed `argc` to `<= 1`, since argc will always be at least one (from the `./a`) – Amit Jan 06 '11 at 05:03
  • @Amit: Do you understand _why_ this works? Do you understand _why_ your approach was not working? – James McNellis Jan 06 '11 at 05:05
  • @James: I do. But I also see that learning C without a book will be impossibly difficult and inefficient. I looked at your post with recommended books and I plan on getting one of the beginner ones. Thank you all for your help in this question! – Amit Jan 06 '11 at 05:09
  • This is VERY incorrect. First, argc is always equal or greater than one. Second, the variable a is used but not declared, so the code won't even compile. Third, even if you declare a, the code segfaults. – Stephen Melvin Jan 06 '11 at 05:10
  • @Steve: I've posted that code that compiles and worked for me under EDIT 3 – Amit Jan 06 '11 at 05:12