In CPP - Loops - ABCD Pyramid Pattern video sir has written ch = ch + 1; (for ch = ‘A’)
Is there some process in c++ that automatically recognises the above statement and converts ch into its decimal ascii equivalent because I think that such things don’t happen in python ?
Character datatype
ch=“A”
ch=ch+1
The above code will cause an error in python
But
char ch=‘A’;
ch =ch+1;
is valid in c++ (so what factor does this?)
Hi @Rooopak_Sharma, the fact that this is happening is that because we pass a number in the variable ch.
ch = “A” actually assigns it’s ascii value to “A” i.e. is 65. Now we can perform algebraic operations on it and when it will be printed the output will be the character associated to that ASCII value. It’s primary use will be as a character but can also be used as an int.
In python this thing does not happen and we cannot do this kind of thing normally. However we can to it using type casting by.
ch = ‘A’
ch = str(int(ch)+1)
Hope this helps