Add/Sub/Multiply/Division of characters, integers etc.

Hi I want to know what happens if you declare a and b as an integer, but you assign a character value to it. For example A is declared as an integer, but you assign '5' to it. What will be the answer? Can someone explain to me what is the value of 'ans' in the following few scenarios and how did you arrive to it? Thanks in advance!

int main()
{
int a,b;
char ans;
a = '5';
b = '7';
ans = a * b;
cout << ans;
return 0;
}

int main()
{
char a,b;
char ans;
a = '5';
b = '7';
ans = a * b;
cout << ans;
return 0;
}

int main()
{
int a,b;
int ans;
a = '5';
b = '7';
ans = a + b;
cout << ans;
return 0;
}

int main()
{
char a,b;
int ans;
a = 5;
b = 7;
ans = a * b;
cout << ans;
return 0;
}
int main()
{
char a;
int ans, b;
a = 5;
b = 7;
ans = a * b;
cout << ans;
return 0;
}
Last edited on
http://en.cppreference.com/w/cpp/language/ascii


Have a look at this, a char is really a small int.

Why don't you run your programs and find out?

Please always use code tags:

http://www.cplusplus.com/articles/z13hAqkS/
Topic archived. No new replies allowed.