Thread: Convert char to int in C and C++

  1. #1
    Registered User
    Join Date
    Apr 2020

    Convert char to int in C and C++

    Hello All, Can anyone know How do I convert a char to an int in C and C++?

    I have checked this <<crappy site removed>> to check how I convert it and its difference to read the value as an ascii code, I have wrote
    [COLOR=var(--highlight-literal)]char[/COLOR][COLOR=var(--highlight-color)] a = [/COLOR][COLOR=var(--highlight-variable)]'a'[/COLOR][COLOR=var(--highlight-color)];[/COLOR][COLOR=var(--highlight-literal)]int[/COLOR] ia = ([COLOR=var(--highlight-literal)]int[/COLOR])a;  [COLOR=var(--highlight-comment)]/* note that the int cast is not necessary -- int ia = a would suffice */[/COLOR]
    to convert the character '0' -> 0, '1' -> 1, etc,

    [COLOR=var(--highlight-literal)]char[/COLOR][COLOR=var(--highlight-color)] a = [/COLOR][COLOR=var(--highlight-variable)]'4'[/COLOR][COLOR=var(--highlight-color)];[/COLOR][COLOR=var(--highlight-literal)]int[/COLOR] ia = a - [COLOR=var(--highlight-variable)]'0'[/COLOR];
    [COLOR=var(--highlight-comment)]/* check here if ia is bounded by 0 and 9 */[/COLOR]

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    New York, NY
    First of all, please post your code as "Plain Text" without all the COLOR and other formatting. Then we can assist you.


    If you are attempting to learn from painfully inadequate online tutorials, (Including the link you provided) YouTube videos, or some book claiming to teach you in a short time, then THROW THEM AWAY!

    Short of taking a course in C Programming from a qualified instructor, you need to study a good book on the C Programming Language, cover to cover, and do all the exercises at the end of each chapter! Choose one of the three listed below:

    C Programming, A Modern Approach
    Author: K. N. King

    C Programming: A Modern Approach - Second Edition

    C Primer Plus, 6th Edition
    Stephen Prata

    C How to Program, 8/e

    C How to Program, 8/e | Deitel & Associates, Inc.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    ankitdixit is a shill for interviewbit
    This is your last warning ankitdixit - one more post from you with your spammy site will be your last.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-09-2013, 07:06 PM
  2. strcat - cannot convert char to const char
    By ulillillia in forum C Programming
    Replies: 14
    Last Post: 12-07-2006, 10:00 AM
  3. Replies: 3
    Last Post: 12-06-2006, 06:59 PM
  4. Newb Question - Cant convert Char[41] to Char
    By Kalkran in forum C++ Programming
    Replies: 2
    Last Post: 08-19-2004, 10:05 AM
  5. Replies: 6
    Last Post: 04-14-2002, 11:25 AM

Tags for this Thread