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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:02:21+00:00 2026-05-11T17:02:21+00:00

If I want to convert a single numeric char to it’s numeric value, for

  • 0

If I want to convert a single numeric char to it’s numeric value, for example, if:

char c = '5';

and I want c to hold 5 instead of '5', is it 100% portable doing it like this?

c = c - '0';

I heard that all character sets store the numbers in consecutive order so I assume so, but I’d like to know if there is an organized library function to do this conversion, and how it is done conventionally. I’m a real beginner 🙂

  • 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-11T17:02:21+00:00Added an answer on May 11, 2026 at 5:02 pm

    Yes, this is a safe conversion. C requires it to work. This guarantee is in section 5.2.1 paragraph 2 of the latest ISO C standard, a recent draft of which is N1570:

    Both the basic source and basic execution character sets shall have the following
    members:
    […]
    the 10 decimal digits
    0 1 2 3 4 5 6 7 8 9
    […]
    In both the source and execution basic character sets, the
    value of each character after 0 in the above list of decimal digits shall be one greater than
    the value of the previous.

    Both ASCII and EBCDIC, and character sets derived from them, satisfy this requirement, which is why the C standard was able to impose it. Note that letters are not contiguous iN EBCDIC, and C doesn’t require them to be.

    There is no library function to do it for a single char, you would need to build a string first:

    int digit_to_int(char d)
    {
     char str[2];
    
     str[0] = d;
     str[1] = '\0';
     return (int) strtol(str, NULL, 10);
    }
    

    You could also use the atoi() function to do the conversion, once you have a string, but strtol() is better and safer.

    As commenters have pointed out though, it is extreme overkill to call a function to do this conversion; your initial approach to subtract ‘0’ is the proper way of doing this. I just wanted to show how the recommended standard approach of converting a number as a string to a “true” number would be used, here.

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

Sidebar

Related Questions

I want to convert from char representing a hexadecimal value (in upper or lower
I want to convert a primitive to a string, and I tried: myInt.toString(); This
I want to convert a string into a double and after doing some math
I want to convert for example a particular date 12-11-2008 11:33:04.510 to UTC datetime.
I want to convert a single character of a string into an integer, add
I have an IEnumerable<IEnumerable<T>> collection that I want to convert to a single dimension
i have an array of events: IEnumerable<CalendarEvent> events i want to convert this to
I Want convert bit type to Yes or No For Example: SELECT FirstName, LastName,
I want to convert an instance of generic IDictionary<,> to non generic IDictionary .
I want to convert a number that is in PRTime format (a 64-bit integer

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.