Conversion of char to int

hari02

hari02

@hari02-jc8atV Oct 25, 2024
Hey guys,
I have been solving a problem post-fix expression evaluation using stacks for lab in the weekend and encountered a problem "conversion of character data to int". I searched through google and found these results "atoi and typecasting".
Both the above conversions are known to me but I came across something different like this
char i='5';
int j = i-'0';
in 'j' the integer value number:5 is stored can anyone please explain how its working

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Manish Goyal

    Manish Goyal

    @manish-r2Hoep Feb 14, 2011

    i think here intenal type casting is going on

    when you write int j=i-'0', compiler takea ascii value of '5' i.e 53 and '0' which is 48 ,
  • hari02

    hari02

    @hari02-jc8atV Feb 14, 2011

    Ya..................Thanx Goyal 😁
  • HirenBarbhaya

    HirenBarbhaya

    @hirenbarbhaya-04r651 Feb 17, 2011

    goyal420
    i think here intenal type casting is going on

    when you write int j=i-'0', compiler takea ascii value of '5' i.e 53 and '0' which is 48 ,
    Awesome Explanation...
  • rohitrk

    rohitrk

    @rohitrk-oRDa7U Feb 17, 2011

    variable i stores the acii value of '5'. when statement j=i-'0' is executed the char '5' is converted to number 5.
    this is one of the ways we use to convert a number from its ascii value to integer value so that arithmetic operations can be performed on it.