Originally Posted by
itsme86
A variable of type char is basically a 1-byte integer. A character constant is just a way to translate a character to the character map value you're working in.
For example, in ASCII, the letter 'A' has a value (decimal) of 65. So instead of doing:
Code:
char ch = 65; // Using ASCII value
...you can do:
Code:
char ch = 'A'; // Using character constant
Both methods end up setting the variable ch to the exact same value (given that you're in an ASCII environment).
Thank you itsme. It was very helpful, indeed.
Originally Posted by
Mozza314
You can, but it's generally not a very good idea. Why would you want to?
Hi Mozza
I was just curious. You are right. It does work. You know some much! I envy you.
Please don't forget that I'm just a beginner.
This the test code, CODE 1, I used to use operator "+" on the characters. (OK, sorry if there is a problem with the indentation! ).
CODE 1:
In the CODE 1 and others I used function "system()". I was told its windows specific (and perhaps not Standard C++ feature), and I have to include header file "#include <windows.h>". But I didn't use the header file "windows.h" anywhere and it still worked. Any reason?
CODE 2:
I have used "float b = 1.54F;". It works fine even when "1.54" isn't followed by "F". Then, why do they include "F" after the floats. They don't use, say "I", in such cases as "int b = 1.54I;. Do you get me?
CODE 3:
Even when I use "float b = 1F;" instead "float b = 1.6F;" it works though I get an error "invalid suffix "F" on integer constant".
I also get warning "[Warning] converting to `int' from `float'". I think it's an automatic conversion. Because final output "x" is declared to be "int" therefore result is converted to "int" value even when it is in "float"
What is general order of precedence of math operators? In general algebra first the expression within parenthesis is solved. Between math operators "division or /" is given the highest precedence, then multiplication, then addition, and finally subtraction. I see there is fifth operator in C++ which is remainder "%". Could you please shed some light on this?
CODE 1:
Code:
// test program on character variables and constants CODE 1
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
cout << 'A' + 'A' << endl; //answer would be 130
system("pause");
}
CODE 2:
Code:
// test program on character variables and constants CODE 2
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
system("color 1a");
int x;
float b = 1.54F;
x = 'A' + b; /* there would be warning like "converting to int from float" */
cout << x << endl; //answer would be 66
system("pause");
}
CODE 3:
Code:
// test program on character variables and constants CODE 3
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
system("color 1a");
int x;
float b = 1.6F;
x = 'A' + b; /* there would be warning like "converting to int from float" */
cout << x << endl; //answer would be 66
system("pause");
}