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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:51:49+00:00 2026-05-16T17:51:49+00:00

I am reading chapter 2 of Advanced Linux Programming: http://www.advancedlinuxprogramming.com/alp-folder/alp-ch02-writing-good-gnu-linux-software.pdf In the section 2.1.3

  • 0

I am reading chapter 2 of Advanced Linux Programming:
http://www.advancedlinuxprogramming.com/alp-folder/alp-ch02-writing-good-gnu-linux-software.pdf

In the section 2.1.3 Using getopt_long, there is an example program that goes a bit like this:

int main (int argc, char* argv[]) {
  int next_option;
  // ...
  do {
    next_option = getopt_long (argc, argv, short_options, long_options, NULL);
    switch (next_option) {
       case ‘h’: /* -h or --help */
       // ...
    }
    // ...

The bit that caught my attention is that next_option is declared as an int. The function getopt_long() apparently returns an int representing the short command line argument which is used in the following switch statement. How come that integer can be compared to a character in the switch statement?

Is there an implicit conversion from a char (a single character?) to an int? How is the code above valid? (see full code in linked pdf)

  • 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-16T17:51:49+00:00Added an answer on May 16, 2026 at 5:51 pm

    Neither C nor C++ have a type that can store “characters” as values with some dedicated character-specific properties. In that sense, there’s no “character” type neither in C nor in C++.

    In both C++ and C languages char is an integral type. It contains numbers. It is just a smallest (in terms of range) integral type. Conversion between char and int exists, just like it exists between int and long or int and short. char has no special status among other integral types (aside from the fact that char type it is distinct from signed char type).

    A literal of the form 'h' in C++ has type char, but as any other integral type it is comparable to int. That’s why you can use it in case label the way it is used in your original example.

    In other words, your original code is as “strange” as

    switch (next_option) {
           case 1L: ...
           // ...
        }
    

    would be. In this case the switch argument is an int, but the case label is a long. The code is valid. Do you find it surprising? Probably not. Your example with 'h' is in not much different.

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

Sidebar

Related Questions

While reading SICP I came across logic programming chapter 4.4. Then I started looking
I am reading the book Javascript: The good parts. Now I am reading chapter
Guys from book C++ GUI programming with qt I'm reading a chapter on how
I am reading "Advanced Mac OS X Programming" by Mark and Aaron. I can't
I've been reading the chapter on functional inheritance in Crockford's 'The Good Parts'. In
I am reading Programming In Haskell, in the 8th chapter, the author gives an
I'm reading a book called PHP 5 Advanced Techniques by Larry Ullman. In Chapter
I am reading Essential COM and encountered a macro 'BASE_OFFSET' from chapter 2 of
I had a question... I'm reading Cocoa Programming for Mac OSX and in Chapter
I am reading Chapter 17 of C Primer Plus, and here is the code

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.