Thread: character constant and character variable

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #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.

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