Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3423482
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:21:37+00:00 2026-05-18T06:21:37+00:00

I am trying to parse the arguments in the command line using switch and

  • 0

I am trying to parse the arguments in the command line using switch and getopt(). The structure is very easy: I have a makefile, a .c and a .h file. This is the first time that I am using switch, so I might be doing some basic mistakes. I have used this link as my guide to switch
link text
and
link text
If you see any basic mistakes please let me know.

makefile:

make:lunar

lunar: lunar.o
    gcc -Wall -std=c99 -g -o lunar lunar.o -lm
lunar.o: lunar.c lunar.h
    gcc -Wall -std=c99 -g -c lunar.c

clean:
    -rm -f *.o lunar core

/////////////////////////////////////

lunar.c

int main (int argc,  char *argv[]){
    int i;
    int c = 0;
    int gravity = 0;
    int thrust = 0;
    opterr = 0;
    while ((c = getopt (argc, argv, "gtf:")) != -1)
        switch (c){
            case 'g':
                gravity = argv[optind];
                break;
            case 't':
                thrust = argv[optind];
                break;
            case 'f':
                argument = argv[optind];
                break;
            case '?':
                if (optopt == 'c')
                    fprintf(stderr, "Option -%c requires 
                            an argument.\n", optopt);
                else if (isprint (optopt))
                    fprintf (stderr, "Unknown option 
                             `-%c'.\n", optopt);
                else
                    fprintf (stderr, "Unknown option 
                             character `\\x%x'.\n", 
                             optopt);
                return 1;
            defult:
                abort ();
        }
    printf ("gravity is %d and thrust is %d.\n", 
            gravity, thrust);
    for (int index = optind ; index < argc ; index++ ){
        printf ("Non-option argument %s\n", argv[index]);
        return 0;
    }
}

///////////////////////////////////

lunar.h

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <math.h>
#include <unistd.h>
#include <assert.h>

#define MAX_WORD 256
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T06:21:37+00:00Added an answer on May 18, 2026 at 6:21 am

    Don’t forget that ‘default‘ has an ‘a’ in it – you’ve just labelled that code with the (unused) label defult, rather than given the switch a default case.

    It is not clear why you think optopt could be ‘c’ or should be treated specially when it is ‘c’; it might be a hangover from an earlier edition.

    You are processing the ‘g’ and ‘t’ options as if they take arguments, but the call to getopt() does not list colons after them. If the switch is to be believed, you need the argument to be "g:t:f:".

    Also, you should be using the char * called optarg instead of trying to use argv[optind]. You use optind after the loop has finished to process extra (‘file name’) arguments.

    Also, since gravity and thrust are integers, you need to convert the strings into integers. The easy way (largely ignoring possible errors) is with:

    gravity = atoi(optarg);
    

    If you want to do error handling, call a function to do the checking and error reporting.

    I recommend putting braces around the switch statement, or around the body of the while statement:

    while ((c = getopt(argc, argv, "g:f:t:")) != -1)
    {
        switch (c)
        {
        ...
        }
    }
    

    It isn’t strictly necessary, but it is (IMNSHO) easier to read the code with the extra braces. For a single line statement, I’d have no problem; but for a complex statement like the switch, I recommend the extra braces.

    The ‘return 0;‘ should be outside the for loop. At the moment, the loop stops on the first non-option argument.


    The makefile is basically fine. Eventually, you’ll use more macros and more compilation warning flags, but the only problems that I spotted that the compiler would also spot were the typo in the default case and the non-conversion of strings into integers.

    PROGRAM = lunar
    SOURCE  = lunar.c
    HEADER  = ${SOURCE:.c=.h}
    OBJECT  = ${SOURCE:.c=.o}
    CFLAGS  = -std=c99 -Wall -g
    LDFLAGS =
    LDLIBS  = -lm
    
    all: ${PROGRAM}
    
    ${PROGRAM}: ${OBJECT}
        ${CC} ${CFLAGS} -o $@ ${OBJECT} ${LDFLAGS} ${LDLIBS}
    
    ${OBJECT}: ${SOURCE} ${HEADER}
        ${CC} ${CFLAGS} -c ${SOURCE}
    
    clean:
        -rm -f *.o ${PROGRAM} core
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to parse command line arguments in an F# application. I'm using pattern
I'm trying to use optparse to parse command line arguments. I would like my
I'm trying to parse an INI file using C++. Any tips on what is
I have a simple Ruby program which takes the command line arguments and posts
I have been trying to parse Java exceptions that appear in a log for
I am trying to parse JSON in an Adobe Flex app, using http://www.mikechambers.com/blog/2006/03/28/tutorial-using-json-with-flex-2-and-actionscript-3/'>This Tutorial
I'm trying to parse some JSON using the JSon.Net library. The documentation seems a
I'm trying to parse a section of a large file with Java's Scanner library,
I'm trying to parse a grammar in ocamlyacc (pretty much the same as regular
I'm trying to parse objects to XML in Delphi, so I read about calling

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.