Is it ok to compare symbols with each other?
#include <iostream>
using namespace std;// For Example, Why if "k = 4" it outputs "r o" ? //
int main() {
char word[] = "programming";
int k;
cin >> k;
for (int i = 0; i < k; i )
if (word[i] > word[i 1]) {
cout << word[i] << endl;
}
}
CodePudding user response:
Is it ok to compare symbols with each other?
Yes, it is OK.
Why if "k = 4" it outputs "r o" ?
Character types are integers. Each integer value of the character type is mapped to a symbol1. This mapping is called a character set or character encoding.
In the character encoding of the system where you ran the program, the value of the character that maps to the symbol 'r' has a greater value than the character that maps to the symbol 'o', while that 'o' character has smaller value than the character that maps to 'g'.
1 This is a simplification. There are special non-printable charcters such as null terminator which aren't symbols as such. Furthermore, the mapping isn't so simple in case of variable length encodings (Unicode).
CodePudding user response:
The char
data type is an integral type, meaning the underlying value is stored as an integer. Moreover, the integer stored by a char
variable is intepreted as an ASCII character.
ASCII specifies a way to map english characters(and some other few symbols) to numbers between 0
and 127
. That is, each english character(and some other few symbols) has a corresponding number between 0
and 127
. This number is formally called a code point.
For example, the code point for the english character a
is 97
. Similarly, the code point for the english character H
is 72
. You can find the list of code points for all the characters here.
The important thing to note here is that the underlying value of a char
variable is stored as an integer. Lets take some examples to clarify this,
char var1 = 'a'; //here var1 is stored as the integer 97
char var2 = 'H'; //here var2 is stored as the integer 72
In the above snippet, var1
is stored as the integer 97
because the code point for the english character a
is 97
. Similarly, var2
is stored as the integer 72
because the english character H
corresponds to the code point 72
.
Now lets come back to your original question. In particular what happens when k =4
.
For k = 4
, the for
loop will be executed 4
times.
Iteration 0: Here i = 0
The if
block basically translates to:
if (word[0] > word[0 1]) {
cout << word[0] << endl;
}
which is:
if ('p' > 'r') {
cout << 'p' << endl;
}
which is(using the ascii table):
if (112 > 114) {
cout << 'p' << endl;
}
since the condition inside if
is false
, the body of the if
block will not be executed and you'll get no output.
Iteration 1: Here i = 1
The if
block basically translates to:
if (word[1] > word[1 1]) {
cout << word[1] << endl;
}
which is:
if ('r' > 'o') {
cout << 'r' << endl;
}
which is(using the ascii table):
if (114 > 111) {
cout << 'r' << endl;
}
since the condition inside if
is true
, the body of the if
block will be executed and you'll get r
as output(which is followed by a newline).
Iteration 2: Here i = 2
The if
block basically translates to:
if (word[2] > word[2 1]) {
cout << word[2] << endl;
}
which is:
if ('o' > 'g') {
cout << 'o' << endl;
}
which is(using the ascii table):
if (111 > 103) {
cout << 'o' << endl;
}
since the condition inside if
is true
, the body of the if
block will be executed and you'll get o
as output(which is followed by a newline).
Iteration 3: Here i = 3
The if
block basically translates to:
if (word[3] > word[3 1]) {
cout << word[3] << endl;
}
which is:
if ('g' > 'r') {
cout << 'g' << endl;
}
which is(using the ascii table):
if (103 > 114) {
cout << 'g' << endl;
}
since the condition inside if
is false
, the body of the if
block will not be executed and you'll get no output.
Hence you get the output:
r
o