String to Int

Can I Convert a char to an int?

Can you convert a char to an int in C++? Yes. Learn why '7' is not the number 7, how to get its digit value, and which common mistakes to avoid.

On this page
  1. Short answer
  2. A char is a small integer
  3. Getting the character code
  4. Getting the digit value
  5. The mistake to avoid
  6. Casting explicitly
  7. Signedness and other values
  8. When you need more than one character
  9. Summary

Short answer

Yes. In C and C++, char is an integer type, so you can convert it to an int with an assignment, a cast, or arithmetic. What you get depends on what you want: the character’s code, or the digit it stands for.

A char is a small integer

A char holds a character code. On systems that use ASCII, the letter 'A' is stored as 65. Because char is an integer type, it converts to int without any special function.

Getting the character code

Assigning a char to an int gives you its code:

char letter = 'A';
int code = letter;  // 65 on systems that use ASCII

Getting the digit value

If the char holds a digit from '0' to '9', subtract '0'. The language requires the digit characters to have consecutive values, so ch - '0' gives the digit’s value for each one:

#include <iostream>

int main() {
    char ch = '7';
    int code = ch;         // the character code, 55 on ASCII systems
    int digit = ch - '0';  // the number 7

    std::cout << code << ' ' << digit << '\n';  // prints 55 7
}

The mistake to avoid

int n = '7'; doesn’t store 7. The single quotes make it a character, so n holds the character’s code, which is 55 on ASCII systems. To turn a digit character into its number, use subtraction as shown above.

Casting explicitly

You can write the conversion explicitly. In C++, static_cast<int>(ch) does the same thing as int code = ch;. In C, (int)ch works. An explicit cast can make the intent clearer to someone reading the code.

Signedness and other values

Whether char is signed depends on the compiler and platform. For digits and common letters, that difference doesn’t matter. It matters for values outside ASCII, such as bytes above 127. For the rules on signed and unsigned values, see signed vs unsigned in c. If you’re handling raw bytes, reading them as unsigned char avoids surprises.

When you need more than one character

A single char is one job. A whole string of digits is another, and it needs a different approach. For that, see c convert string to int. For a broader walkthrough of char-to-int conversion, read the c char to int guide.

Summary

Yes, a char converts to an int. Assigning it gives you the character code, and subtracting '0' gives you a digit’s value. Keep those two results apart, and remember that '7' is not the number 7.

The complete guide to string to int

How to Convert a String to an Int in C++
Read the guide

Keep exploring

Guides from other topics

All topics →

The inline Keyword in C++

Learn how the inline keyword in C++ works, why inline functions belong in header files, how inline variables work in C++17, and when the compiler ignores it.

inline 3 min read

C++ Header File Example

A complete C++ header file example: a .h file with declarations, a .cpp file with definitions, a main program, and how to compile and run them.

Header Files 3 min read