Get number from pattern (a,b)?

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 682 Views - Last Post: 30 May 2012 - 11:27 AM Rate Topic: -----

#16 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,176
  • Joined: 08-January 12

Re: Get number from pattern (a,b)?

Posted 30 May 2012 - 10:37 AM

Not exactly. If you type a-48, it will mean 97-48 that is 49. As I mentioned earlier also, ASCII value of a is 97. So if you want 2 you type '2'-48 which will give 2. I have used '' because they indicate 2 is char, not int.
EDIT : Now that I am back, let me explain in detail. Since you are typing
printf("%d",a-48);


It tells the compiler to print the integral value of 97-48, since the ASCII value of a is 97. If you want integer 2, either write
printf("%d",'2'-48);//Note the '2', which tell it is char 2, having ASCII value 50.


or
printf("%d",a-95);//95 because 97-95=2.


I see many people have doubt about ASCII value. So is there any tutorial on DIC about the same ??

This post has been edited by aresh: 30 May 2012 - 10:52 AM

Was This Post Helpful? 0
  • +
  • -

#17 tomas7470  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 54
  • Joined: 19-February 09

Re: Get number from pattern (a,b)?

Posted 30 May 2012 - 11:24 AM

U got me Wrong!!
I was ment that a is a number in char!
like im doing:
scanf("%c", a);
a = '2';
printf("%d", a-48);


Was This Post Helpful? 0
  • +
  • -

#18 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,176
  • Joined: 08-January 12

Re: Get number from pattern (a,b)?

Posted 30 May 2012 - 11:25 AM

Oh sorry. Yup, you are right :D
Was This Post Helpful? 0
  • +
  • -

#19 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Get number from pattern (a,b)?

Posted 30 May 2012 - 11:26 AM

No. 'a' is 97, so if you do this
printf("%d\n", (int)('a' - 48));

it will print 49.

Try it.

But
char a = '2';
printf("%d\n", (int)(a-48));


will print 2.

This post has been edited by r.stiltskin: 30 May 2012 - 11:28 AM

Was This Post Helpful? 0
  • +
  • -

#20 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,176
  • Joined: 08-January 12

Re: Get number from pattern (a,b)?

Posted 30 May 2012 - 11:27 AM

Umm r.stiltskin, please look at the previous posts.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2