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

  • Home
  • SEARCH
  • 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 8847665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:14:39+00:00 2026-06-14T12:14:39+00:00

I have a simple test program (error checks removed): #include <iostream> #include <iomanip> #include

  • 0

I have a simple test program (error checks removed):

#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>

int main() {
    std::string line;
    while(std::cin >> line) {
        int value;
        std::stringstream stream(line);

        stream >> std::setbase(0) >> value;

        std::cout << "You typed: " << value << std::endl;
    }

}

Which works great for prefix-dependent integer parsing. It’ll parse strings starting with "0x" or "0X" as hexadecimal and strings starting with '0' as octal. This is explained in several resources that I use and have seen. What I haven’t been able to find though, is an indication in the C++ standard that this is guaranteed to work.

Section 7.20.1.4.3 on strtol in the C standard says (6.4.4.1 is the syntax for integer constants) I imagine the extraction operators use this under the hood:

If the value of base is zero, the expected form of the subject sequence is that of an
integer constant as described in 6.4.4.1, optionally preceded by a plus or minus sign, but
not including an integer suffix.

This works on the couple of versions of GCC that I’ve tried, but is it safe to use generally?

  • 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-06-14T12:14:41+00:00Added an answer on June 14, 2026 at 12:14 pm

    setbase is defined in C++98 [lib.std.manip]/5, paraphrasing slightly

    smanip setbase(int base);
    

    Returns: An object s of unspecified type such that [inserting or extracting s from a stream behaves as if the following function were called on that stream:]

    ios_base& f(ios_base& str, int base)
    {
        str.setf(n == 8 ? ios_base::oct :
                 n == 10 ? ios_base::dec :
                 n == 16 ? ios_base::hex :
                 ios_base::fmtflags(0), ios_base::basefield);
        return str;
    }
    

    Okay, so, if base is not 8, 10, or 16, then the basefield flags are cleared. The effect of a cleared basefield for input is defined in [lib.facet.num.get.virtuals], table 55 (“Integer conversions”) as equivalent to sscanf("%i") on the sequence of characters next available.

    C++98 refers to C89 for the definition of *scanf, naturally enough. I don’t have a PDF copy of C89, but I do have C99, in which section 7.19.6.2 paragraph 12 [the C standard does not have the nice symbolic section names that the C++ standard has] defines "%i" to behave the same as strtol with base argument 0.

    So the good news is, prefix-dependent integer scanning is guaranteed by the standard after setbase(0). The bad news is, iostream formatted input is defined in terms of *scanf, which means the dreadful sentence at the end of C99 7.19.6.2p10 applies:

    If [the object that receives the result of scanning] does not have an appropriate type, or if the result of the conversion cannot be represented in the object, the behavior is undefined.

    (Emphasis mine.) Clearer version of that sentence: input overflow triggers undefined behavior. The C(++) runtime is allowed to crash the program if input to *scanf has too many digits! This is (one of several reasons) why I and others keep saying *scanf should never be used, and now I have to start saying it about istream >> int as well. 🙁

    The advice that holds for C is even easier to apply in C++: Read entire lines with std::getline and parse them by hand. Use the strtol family of functions to convert numeric input to machine numbers. (Those functions have predictable behavior on overflow.)

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

Sidebar

Related Questions

I have a very simple test program, running on Solaris 5.8: #include <stdio.h> #include
I have a simple test program that when I run I get: ./hello: error
I have a simple RabbitMQ test program randomly enqueuing messages, and another reading them,
I have a strange build problem. I have a simple test program that sends
I have a simple test program which is designed to consume the Apache Felix
I have been trying to get a simple networking test program to run with
I have a made a simple program that reproduces the problem: #include <boost/archive/text_oarchive.hpp> #include
This is just a simple test program. I'm trying to have the Arduino print
I have a simple test case failing in Django: Model (app/models.py): from django.db import
I have a simple test application (C# console application) that does an HTTP GET

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.