Depends on what you want to do:
to read the value as an ascii code, you can write
char a = 'a'; int ia = (int)a; /* note that the int cast is not necessary -- int ia = a would suffice */
to convert the character '0' -> 0
, '1' -> 1
, etc, you can write
char a = '4'; int ia = a - '0'; /* check here if ia is bounded by 0 and 9 */
Explanation:a - '0'
is equivalent to ((int)a) - ((int)'0')
, which means the ascii values of the characters are subtracted from each other. Since 0
comes directly before 1
in the ascii table (and so on until 9
), the difference between the two gives the number that the character a
represents.
Related Posts:
- What exactly is the difference between “pass by reference” in C and in C++?
- Fatal error: iostream: No such file or directory in compiling C program using GCC
- gcc/g++: “No such file or directory”
- Debug vs Release in CMake
- Debug vs Release in CMake
- gcc: undefined reference to
- gcc -g :what will happen
- cc1.exe System Error – libwinpthread-1.dll missing – But it isn’t
- Fatal error: iostream: No such file or directory in compiling C program using GCC
- Linker error: “linker input file unused because linking not done”, undefined reference to a function in that file
- What is a segmentation fault?
- How many spaces for tab character(\t)?
- What does (~0L) mean?
- What is the effect of extern “C” in C++?
- Why are #ifndef and #define used in C++ header files?
- When to use extern “C” in simple words? [duplicate]
- Floating point exception( core dump
- Mutex example / tutorial? [closed]
- What does “dereferencing” a pointer mean?
- What is an unsigned char?
- What does “dereferencing” a pointer mean?
- Why use conio.h?
- How to track down a “double free or corruption” error
- How to track down a “double free or corruption” error
- How to go from fopen to fopen_s
- Undefined reference to vtable
- Difference between long double and double in C and C++ [duplicate]
- What is use of c_str function In c++
- What is the difference between const int*, const int * const, and int const *?
- What is the C version of RMI
- How to print pthread_t
- Undefined reference to vtable
- C ++ error: a expected initializer before [function name]
- Inheriting constructors
- Fastest way to check if a file exist using standard C++/C++11,14,17/C?
- warning: incompatible implicit declaration of built-in function ‘xyz’
- Difference between using Makefile and CMake to compile the code
- What is the difference between a static and const variable?
- Should I learn C before learning C++?
- Eclipse C++ : “Program “g++” not found in PATH”
- Visual Studio debugger error: Unable to start program Specified file cannot be found
- unsigned int vs. size_t
- MinGW: “gcc is not recognized as an internal or external command”
- Convert Python program to C/C++ code?
- error: expected primary-expression before ‘)’ token (C)
- Is “argv[0] = name-of-executable” an accepted standard or just a common convention?
- What’s the equivalent of new/delete of C++ in C?
- What is activation record in the context of C and C++?
- Compiling a C++ program with gcc
- Fastest way to check if a file exist using standard C++/C++11,14,17/C?
- conflicting types error when compiling c program using gcc
- What is a file with extension .a?
- How to convert C++ Code to C
- How to make an array with a dynamic size? General usage of dynamic arrays (maybe pointers too)?
- Fatal error: ‘stdafx.h’ file not found
- warning: control reaches end of non-void function [-Wreturn-type]
- What does ‘const static’ mean in C and C++?
- error: use of deleted function
- Best C/C++ Network Library
- Is the sizeof(some pointer) always equal to four?
- 2D array vs array of arrays
- How do malloc() and free() work?
- C++ array assign error: invalid array assignment
- CMake error at CMakeLists.txt:30 (project): No CMAKE_C_COMPILER could be found
- How to run valgrind with basic c example?
- Typedef function pointer?
- cannot declare variable ‘’ to be of abstract type ‘’
- “…redeclared as different kind of symbol”?
- What is the use of intptr_t?
- How can I get the list of files in a directory using C or C++?
- What does “Permission denied” “Id returned 1 exit status” mean?
- What is the C equivalent to the C++ cin statement?
- Static linking vs dynamic linking
- What are the different versions of exec used for in C and C++?
- What’s the difference between nexti and stepi in gdb?
- C++ – Too Many Initializers for Arrays
- Reading string by char till end of line C/C++
- Is there a replacement for unistd.h for Windows (Visual C)?
- C++ – include unistd.h: why not cunistd?
- Removing trailing newline character from fgets() input
- What does the fpermissive flag do?
- How to project a point onto a plane in 3D?
- C: using strtol endptr is never NULL, cannot check if value is integer only?
- Why use pointers?
- What happens during a “relocation has invalid symbol index” error?
- What’s the difference between * and & in C?
- What does this GCC error “… relocation truncated to fit…” mean?
- Difference between pic Vs pie
- Double pointer array in c++
- What’s the meaning of exception code “EXC_I386_GPFLT”?
- How to write log base(2) in c/c++
- What is the job of autogen.sh when building a c++ package on Linux
- Is there a standard sign function (signum, sgn) in C/C++?
- Warning : overflow in implicit constant conversion
- Using OpenMP with clang
- Warning: comparison of distinct pointer types
- Two decimal places using printf( )
- Make / gcc cryptic error 2: how to have more information?
- Is there a way to compile C++ to C Code?
- Whats the difference between UInt8 and uint8_t