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 7546447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:12:04+00:00 2026-05-30T09:12:04+00:00

I have written a simple code using getopt for understanding perspective. #include <stdio.h> #include

  • 0

I have written a simple code using getopt for understanding perspective.

#include <stdio.h>
#include <unistd.h>

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

 /* Here since c is followed with colon, so 'c' takes an argument */
  const char *optstring = "abc:d";

  int ret;

  while((ret=getopt(argc, argv, optstring))!= -1)
  {
    switch(ret)
    {
      case 'a':
        printf("Option found: a, optind = %d\n",optind);
        break;

      case 'b':
         printf("Option found: b, optind = %d\n",optind);
         break;

      case 'c':
         printf("Option found: c, optind = %d\n",optind);
         printf("Option argument: %s\n",optarg);
         break;

      case 'd':
         printf("Option found: d, optind = %d\n",optind);
         break;

      case ':':
         printf("The option takes an argument which is missing");
         break;

     //case '?':
       //   printf("Didn't you enter an invalid option?");
           // break;
     }
   }
 }

The problem is:

(1) Case 1:
If the case '?' is commented then:

[root@dhcppc0 getopt]# ./a.out -a -b -c
    Option found: a, optind = 2
    Option found: b, optind = 3
    ./a.out: option requires an argument -- c

So, as you can see, the case ':' did not take into effect, as normally we expect a missing argument to return a ‘:’ (colon) by getopt.

(2) Case 2:
AND, if i un-comment it, and then run the program, it hits the case '? even for missing argument.

enter code here
[root@dhcppc0 getopt]# ./a.out -a -b -c
   Option found: a, optind = 2
   Option found: b, optind = 3
      ./a.out: option requires an argument -- c
      Didn't you enter an invalid option?

What is the point i am missing here?

ADDED LATER:

Also why is the ./a.out: option requires an argument -- c the default error coming? How to handle it, since i am already taking care for it in the case ':', and don’t want the default error message?

ADDED AGAIN:
As suggested in the answer, i used the colon in beginning of optstring – const char *optstring = ":abc:d", then why is this happening?

./a.out -a -b -c -d returns -d as the argument to c?? -d is a separate optional character and not any argument
  • 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-30T09:12:05+00:00Added an answer on May 30, 2026 at 9:12 am

    The POSIX version of the getopt() function specifies:

    If getopt() encounters an option character that is not contained in optstring, it shall return the <question-mark> ( ‘?’ ) character. If it detects a missing option-argument, it shall return the <colon> character ( ‘:’ ) if the first character of optstring was a <colon>, or a <question-mark> character ( ‘?’ ) otherwise.

    Since your optstr does not start with a :, it should return a ? instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written some simple code, to send en auto generated email, using the
I have written simple code to determine country location using the suffix on the
I have written a very simple WCF service, that worked fine (code below), then
I have some code I've written in PHP for consuming our simple webservice, which
I have sample code written for parsing xml file using javax.xml package. it uses
I have written a simple test application using asp.net mvc with C#. The application
I have written a simple server - client program with Swing interface using singleton
I have written a simple C program using gcc compiler in Ubuntu enviroment. The
I'm using Struts2, Spring, and Hibernate. I have written a simple Excel file called
I have written simple first stage bootloader which displays Hello world using interrupt to

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.