what does var-'0' means . Please help .Code:#include<stdio.h>
#include<conio.h>
void main()
{
char var;
scanf("%c",&var);
printf("%c\n%d\n%d ",var-'0',(int)var);
getch();
}
Printable View
what does var-'0' means . Please help .Code:#include<stdio.h>
#include<conio.h>
void main()
{
char var;
scanf("%c",&var);
printf("%c\n%d\n%d ",var-'0',(int)var);
getch();
}
Assuming that var is in the range ['0', '9'] (i.e., digit characters), it gives you the corresponding value in the range [0, 9].
The format string expects three parameters.
First, you have one too many arguments in the quotes. Remove the last %d.
Now. If you compile this:
Output:Code:char var = 0;
// scanf("%c",&var);
printf("%c\n%d\n%d", (var-'0') ,(int)var, '0');
\320
0
48
You'll understand that '0' integer representation is 48.
So, I set var to 0 for simplicity. It would be the character representation of 0 - 48. Which gives the character /320 (on this Mac).
Also, I do not believe you needed to type cast var. But it's good practice I suppose.
You are calling the printf function incorrectly... It wants 3 parameters not 2...
Try this...
]Maybe it will make more sense to you.Code:#include<stdio.h>
#include<conio.h>
void main()
{
char var;
scanf("%c",&var);
printf("%c\n%d\n%d ",var,var-'0',(int)var);
getch();
}
Thanks for reply and sorry for extra %d in printf statement.
But tilll the extend I have got the idea here '0' represent zero and we are reducing var ie. character variable var with '0'. So the ASCII code for 0 is 48 and if i give input for var as q.
So, ASCII code of q i.e.113 this means 113-48=65and we get ans as A which have ASCII code 65.
So, indeed this just an arithmetic expression in which ASCII code of character stored in var is reduced by ASCII code of 0 which is 48.