2

Brian Kernighnan in his book Programming with C says

By definition, chars are just small integers, so char variables and constants are identical to ints in arithmetic expressions.

Does this mean we can subtract char variable from int ??

I wrote a small piece of code:

#include <stdio.h>

main()
{
    int a ;
    int c;
    a = 1;
    c = 1 - '0' ;
    printf("%d", c);
}

However it gives me output = -47...

What is that I'm doing wrong ?? Are the variables I assigned have the right type??

ArchKudo
  • 123
  • 1
  • 4
  • 11

5 Answers5

6

The output is to be expected. '0' is a char value that, since your compiler presumably uses the ASCII encoding, has value 48. This is converted to int and subtracted from 1. Which gives the value -47.

So the program does what it is expected to do, just not what you might hope it would do. As for what you are doing wrong, it is hard to say. I'm not sure what you expect the program to do, or what problem you are trying to solve.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Ok this makes it clear why I got answer as -47........But I'm still not sure do you mean to say yes to my question: can I subtract char and int ?? – ArchKudo May 28 '14 at 10:25
  • 1
    @ArchKudo Well yes, you just did. You substracted the char `'0'` from the int `1`. And you got back the int `-47`. – user703016 May 28 '14 at 10:29
  • 1
    @ArchKudo The `char` is implicitly converted to an `int`, and then the subtraction is performed with two `int` values. – David Heffernan May 28 '14 at 10:35
3

The characters from '0'-'9'' have values 48-57 when converted to integer ('0' = 48, '1' = 49 etc). Read more about ASCII Values. When used in numerical calculation, first they are converted to int, so 1- '0' = 1-48 =-47.

Rakib
  • 7,435
  • 7
  • 29
  • 45
2

You're mixing here the actual operation with the form of representation. printf outputs the data according to the specified format - integer in your case. If you want to print it as a character, switch %d with %c.

SomeWittyUsername
  • 18,025
  • 3
  • 42
  • 85
  • But didn't I define c as int so dosen't this mean I have to use %d in printf with respect to c which is an integer ? – ArchKudo May 28 '14 at 10:18
  • @Adriano isn't that what I said?? (Did I Get you wrong??) – ArchKudo May 28 '14 at 10:38
  • @ArchKudo yes, you're right. comment was for icepack! :) – Adriano Repetti May 28 '14 at 11:02
  • @AdrianoRepetti That's exactly the point - `%d` defines the output format of the data you're printing, regardless of the data itself (which is just a block of 0s and 1s fom computer perspective and you choose how to interpret it according to your needs). – SomeWittyUsername May 28 '14 at 11:14
  • @icepack, Oh yes it does convert the int into char,when I had int a = 1 print with %c parameter it gives me SOH, I didn't knew about it thank you........ – ArchKudo May 28 '14 at 12:08
1

What you are doing is treating with the ASCII code of the chars, each char has an ASCII value assigned.

enter image description here

Now, playing a little with the ASCII of each char you can do things like:

int a;
a = 'a' - 'A' ;
printf("%d", a);

And get 32 as output, due to the ASCII value to 'a' = 97 and for 'A' = 65, then you have 97-65 = 32

Roberto
  • 35
  • 6
0

I think this gives you more clear understanding...

#include <stdio.h>

main()
{
    int a ;
    a = 1;
    printf("%d", char(a+48));
//or printf("%d", char(a+'0')); 
}
Jumabek
  • 68
  • 2
  • 7