Thread: Arguments to C console program on Windows

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    9

    Arguments to C console program on Windows

    I'm trying to make a C console program that accepts arguments. Right now I'm checking that all the parameters are read correctly.

    I use codeblocks.

    Here is my code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
        char i = 1;
        printf("Hello world!\n");//Keeping this in to  verify that at least something worked.
        while(argv[i] != NULL)
        {
            printf(argv[i] + '\n');
            i ++;
        }
        return 0;
    }
    With no parameters, it prints normally.
    https://i.imgur.com/AUw6GXi.png

    With a parameter of -b or --b, it does this:
    https://i.imgur.com/WTamUqI.png

    With parameters of --b and --4, it does this:
    https://i.imgur.com/b6aA49u.png

    I assume this has something to do with it accessing system/global variables? What is going on here? How do get it to read the variables passed to it as an 2d array of chars, which I can use in my program? I don't need or want to access system variables, I just need it to read things passed to it as strings which I can then operate on in my program.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    > printf(argv[i] + '\n');
    Because this is nonsense.

    Or more specifically, it's
    printf("%s", &argv[i]['\n'] );

    Try just
    printf("%s\n", argv[i] );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2015
    Posts
    9
    I keep forgetting that C is not Java or Python...

  4. #4
    Registered User
    Join Date
    Dec 2015
    Posts
    9
    I will have at least 5 arguments, but I'm not sure what to do when one argument is present but others aren't.

    Arguments
    1. File to process or path to file (string)
    2. Pattern to search for (string)
    3. Endianness (byte)
    4. Word Size (byte)
    5. Start Address, Inclusive (long)
    6. End Address, Inclusive (long)

    Arguments 1 and 2 are mandatory. I was thinking of making arguments 3 through 6 optional by using defaults, but now I'm not sure how to do that. I can't simply assign a certain index in the array of argument pointers because there's no way to know if that index is a specific argument. Should I just require that the user always provide all the possible arguments (which can be a pain for the user), or is there some other way I can design the argument inputs to avoid this while minimizing wordiness in typing the arguments?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    You look at argc, and decide how many compulsory (or optional) arguments are present.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 11-09-2010, 01:01 AM
  2. msdos character mode console program- multiple windows
    By HowardL in forum Windows Programming
    Replies: 2
    Last Post: 08-06-2007, 12:55 PM
  3. Passing Arguments in windows
    By swanley007 in forum C++ Programming
    Replies: 7
    Last Post: 07-28-2006, 09:42 AM
  4. Console --> windows...
    By pritin in forum Windows Programming
    Replies: 4
    Last Post: 09-21-2005, 05:04 PM
  5. my first windows console program
    By Syneris in forum Windows Programming
    Replies: 3
    Last Post: 04-08-2002, 03:18 PM