Thread: character constant and character variable

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    254

    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.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    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).
    If you understand what you're doing, you're not learning anything.

  3. #3
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Quote Originally Posted by jackson6612 View Post
    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. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by itsme86 View Post
    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.

    Quote Originally Posted by Mozza314 View Post
    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");
     	
    }
    Last edited by jackson6612; 04-05-2011 at 06:56 AM.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    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]
    If you understand what you're doing, you're not learning anything.

  6. #6
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Quote Originally Posted by jackson6612 View Post
    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.
    Last edited by Mozza314; 04-05-2011 at 06:46 PM.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by itsme86 View Post

    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:
    https://docs.google.com/viewer?a=v&p...thkey=CIHDsegP

    And thank you Mozza.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  8. #8
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Quote Originally Posted by jackson6612 View Post
    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 subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM