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

Ask A Question

Stats

  • Questions 102k
  • Answers 102k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Your comparer looks wrong to me. You're still just sorting… May 11, 2026 at 8:12 pm
  • Editorial Team
    Editorial Team added an answer You can do it all in one hit with preg_replace_callback… May 11, 2026 at 8:12 pm
  • Editorial Team
    Editorial Team added an answer That's exactly what you are doing. May 11, 2026 at 8:12 pm

Related Questions

I am working on a project where I can to convert a api digit
In the documentation of hardware that allows us to control it via UDP/IP, I
I want to write a function that accepts a parameter which can be either
I need to convert a passed in argument (single object or collection) to an
I have a menu of product brands that I want to split over 4

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.