Why 97?

Oct 11, 2015 at 9:42pm
Why is the output 97 for a, 98 for b and so on?

1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;

int main(){
int a = 'a';
int b = 'b';
cout << a << endl << b ;

}
Last edited on Oct 11, 2015 at 9:56pm
Oct 11, 2015 at 9:55pm
You are assigning the variable as int, but giving it a CHAR value.
a is ascii code 97 I do believe.
Oct 11, 2015 at 9:55pm
Because the ascii code for a = 97 and the code for b = 98. Look an ascii table and look at a and b. If you want the console to print out the characters 'a' and 'b' then you need to assign them to a variable as a char (not an int).
Oct 11, 2015 at 10:14pm
Ok so why there is no ASCII code for "a" if 'a' and "a" both are character?
Last edited on Oct 11, 2015 at 10:14pm
Oct 11, 2015 at 10:20pm
inside double quotes "a" is a string. Inside single quotes 'a' it is a char. Strings and chars are different in that a string is a bunch of chars (Characters) "Strung" together... It's a different process.
Last edited on Oct 11, 2015 at 10:21pm
Topic archived. No new replies allowed.