# Thread: character constant and character variable

1. ## character constant and character variable

I have a page or two on character variables and character constants but couldn't understand much. Could you please help me? A character is very short range integer. Isn't it?

Can I apply mathematical operations such '+', '*', etc. on characters , why character constant?

Thanks for your help.

2. 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).

3. Originally Posted by jackson6612
Can I apply mathematical operations such '+', '*', etc. on characters , why character constant?
You can, but it's generally not a very good idea. Why would you want to?

4. 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");

}```

5. 1) system() is a standard function. The programs you run via the system() call are what is OS-specific. For example, system("dir") would work in Windows, where as system("ls") would work in Linux.

2) Floats aren't the only thing that get a suffix. For instance, 123ULL indicates that 123 should be treated as an unsigned long long.

3) In mixed-type expressions, the smaller type is automatically promoted to the larger type. In your example 'int = char + float', the char is automatically promoted to a float to perform the addition, so you get 'int = float + float'. The compiler warns you about this because you can't stuff a float into an int. The way to avoid the warning would be to cast the result to the expected type: 'x = (int)('A' + b)' or: 'x = 'A' + (int)b'

For a list of what order different operators have, see here: C++ Operator Precedence [C++ Reference]

6. Originally Posted by jackson6612
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?
Not quite. '/' and '*' have equal precedence in mathematics, and in C++. Although mathematically I guess it doesn't make a difference, as (a * b) / c = a * (b / c). Similarly '+' and '-', they have equal precedence. Also, there are many more than 5 operators in C++, see the link provided by itsme86.

You can see that '/' and '*' have equal precedence in C++ by looking at integer arithmetic. If '/' had higher precedence than '*', then 5 * 1 / 3 would be evaluated as 5 * (1 / 3), and 1 / 3 is 0 integer arithmetic so you get 5 * 0 = 0. However, I think you will find you get 5 * 1 / 3 = 1, as (5 * 1) / 3 = 5 / 3 = 1.

7. Originally Posted by itsme86

2) Floats aren't the only thing that get a suffix. For instance, 123ULL indicates that 123 should be treated as an unsigned long long.

3) In mixed-type expressions, the smaller type is automatically promoted to the larger type. In your example 'int = char + float', the char is automatically promoted to a float to perform the addition, so you get 'int = float + float'. The compiler warns you about this because you can't stuff a float into an int. The way to avoid the warning would be to cast the result to the expected type: 'x = (int)('A' + b)' or: 'x = 'A' + (int)b'
Thanks a lot, itsme. It was really helpful. I think you didn't get my question because of bad phrase so let me rephrase it.

CODE 2:
I have used "float b = 1.54F;". It works fine even when "1.54" isn't followed by "F". "b" is the variable and it's first assigned value is 1.54. Why do I need to float its first temporary value "1.54" separately by using suffix F after it? We don't "I" suffix in such cases as int b = 1.54. Is it any clearer now? Please let me know if there is a problem. Thank you.

And the book I have book uses cast function differently. Please have a look:

And thank you Mozza.

8. Originally Posted by jackson6612
I have used "float b = 1.54F;". It works fine even when "1.54" isn't followed by "F". "b" is the variable and it's first assigned value is 1.54. Why do I need to float its first temporary value "1.54" separately by using suffix F after it? We don't "I" suffix in such cases as int b = 1.54. Is it any clearer now? Please let me know if there is a problem. Thank you.
1.54 will be interpreted as a double constant if you don't qualify it with a suffix, if you then assign that double to a float, it's a simple conversion, so it still works.

Why do I need to float its first temporary value "1.54" separately by using suffix F after it?
What do you mean you need to? You said yourself that it works without it.

Is there a particular reason why you're using floats? I think they're a bit of a waste of time, I always use doubles.

There is no "I" suffix for integer because a whole number in the code is automatically interpreted as a int constant.

Popular pages Recent additions