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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:45:10+00:00 2026-05-20T09:45:10+00:00

Hello all I hope you can help me understand why getopt used an int

  • 0

Hello all I hope you can help me understand why getopt used an int and the handling of the optopt variable in getopt. Pretty new to C++.

Looking at getopt, optopt is defined as an integer.
http://www.gnu.org/software/libtool/manual/libc/Using-Getopt.html#Using-Getopt

and the example here,
http://www.gnu.org/software/libtool/manual/libc/Example-of-Getopt.html#Example-of-Getopt

In this example the part I do not understand is how `c’, an integer is being compared to a char in the switch statement.

As I understand it the main argument geopt is working though is the character array argv so that fact that it deals returns an int seems strange to me, my expectation would be an char and that I would be required to cast any numerical arguments to int. Is the char being automatically converted to it’s ANSI code and back again or something? The printf statment

fprintf (stderr, "Unknown option `-%c'.\n", optopt);

Is expecting a char as I understand it, but being given an int. Why would getopt use int when it’s dealing with a character array?

Am I missing something really obvious? I must be.

  • 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-20T09:45:10+00:00Added an answer on May 20, 2026 at 9:45 am

    When an integer is compared to a character constant as in optopt == 'c', the character constant actually has type int. In fact, in C char and short are second-class citizens and are always promoted to int in expressions, so it’s no use passing or returning a char; it’s promoted to int anyway.

    This is different from the C++ rules, but something you should be aware of when using C functions such as getopt and printf in a C++ program.

    The fact that getopt returns an int has an additional reason: it may return either a valid char value (typically 0..255) or -1. If you want to get a char out of getopt, you have to check for the -1 possibility first:

    int i = getopt(argc, argv, option_string);
    if (i == -1)
        // no more options
    else {
        char c = i;
        // got a valid char, proceed
    }
    

    Had getopt returned a char, then there would be no way to distinguish the possibly valid (char)(-1) (either -1 or 255) from the stop condition. See this page for a better explanation in the context of the EOF value, which is very similar.

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

Sidebar

Related Questions

Hello all you helpful folks @ stackoverflow! Best resources for Java GUI's? Looking at
Hello All, I have the following molten data: X variable value 1 StationA SAR11.cluster
NOTE: Using .NET 2.0, and VS2005 as IDE Hello all, I'm working on logging
$('div.box').html(hello) puts the word hello inside all my div's of class 'box'. They all
All of my codes fail. They should print he, hello and 5 \n 3,
Hullo all, Wondering if there are any Java hackers who can clue me in
Hello can anybody solve this please I'm creating the object in the action class
(edited from original post to change BaseMessage to const BaseMessage&) Hello All, I'm very
Hello! I would like to extract all citations from a text. Additionally, the name
Following code, credit: Guffa. Hello All, I'm trying to add controls to a Form

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.