By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,373 Members | 1,792 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,373 IT Pros & Developers. It's quick & easy.

Assigning data to character variable

P: 6
hello , i've got a problem with C++ , in C++ :there is a type called char and i saw in a book that char only can be ONE character long , not more thn one . and i saw too that the character must be between two ' ' , but there was an integer number that wasn't between two apostrofs: ' ' , why??

thanks for reading this file, plz anser if you would like
Aug 13 '07 #1
Share this Question
Share on Google+
7 Replies


Expert 10K+
P: 11,448
hello , i've got a problem with C++ , in C++ :there is a type called char and i saw in a book that char only can be ONE character long , not more thn one . and i saw too that the character must be between two ' ' , but there was an integer number that wasn't between two apostrofs: ' ' , why??

thanks for reading this file, plz anser if you would like
Using C/C++ a char is just a small number; usually in the interval [0,255]. So
you don't really need to do this:

Expand|Select|Wrap|Line Numbers
  1. char myChar= 'A';
  2.  
you can also do this:

Expand|Select|Wrap|Line Numbers
  1. char myChar= 65;
  2.  
... just because 65 happens to be the character code for 'A' if you're using a machine
that uses ASCII code for its characters.

kind regards,

Jos
Aug 13 '07 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
Get a copy of C++ Primer PLus by Stephen Prata and read the first 9 chapters.

Then ask your question again.
Aug 14 '07 #3

P: 6
but i saw that in the ASCII-codes were too the 0 till 9 ,their codes were: 48 till 57 ,and in this program :
Expand|Select|Wrap|Line Numbers
  1. //program
  2. #include <iostream.h>
  3.  
  4. void main()
  5. [
  6.   char character1 = 'A' , character2 ;
  7.   cout << character1 << endl ;
  8.  
  9.   character2 = character1 + 1 ;
  10.  
  11.   cout << character2 << endl ;
  12.   cin.get()
  13. ]
  14.  
you see that 'A' have the ASCII-code 65, if you do this: character1 + 1, then the code will be 66, and 66 ASCII-code is 'B'.
but if character 1 wasn't 'A' but '9', then what would be the debugging ??here the program not with 'A' but with '9' :
Expand|Select|Wrap|Line Numbers
  1. //program
  2. #include <iostream.h>
  3.  
  4. void main()
  5. [
  6.   char character1 = '9' , character2 ; //you can see that the 9 is with apostrofs
  7.   cout << character1 << endl ;
  8.  
  9.   character2 = character1 + 1 ;
  10.  
  11.   cout << character2 << endl ;
  12.   cin.get()
  13. ]
  14.  
what is the result of this program?? is it this:??

9
:
Aug 14 '07 #4

sicarie
Expert Mod 2.5K+
P: 4,677
but i saw that in the ASCII-codes were too the 0 till 9 ,their codes were: 48 till 57 ,and in this program :
Expand|Select|Wrap|Line Numbers
  1. //program
  2. #include <iostream.h>
  3.  
  4. void main()
  5. [
  6.   char character1 = 'A' , character2 ;
  7.   cout << character1 << endl ;
  8.  
  9.   character2 = character1 + 1 ;
  10.  
  11.   cout << character2 << endl ;
  12.   cin.get()
  13. ]
  14.  
you see that 'A' have the ASCII-code 65, if you do this: character1 + 1, then the code will be 66, and 66 ASCII-code is 'B'.
but if character 1 wasn't 'A' but '9', then what would be the debugging ??here the program not with 'A' but with '9' :
Expand|Select|Wrap|Line Numbers
  1. //program
  2. #include <iostream.h>
  3.  
  4. void main()
  5. [
  6.   char character1 = '9' , character2 ; //you can see that the 9 is with apostrofs
  7.   cout << character1 << endl ;
  8.  
  9.   character2 = character1 + 1 ;
  10.  
  11.   cout << character2 << endl ;
  12.   cin.get()
  13. ]
  14.  
what is the result of this program?? is it this:??

9
:
The result of that program is a lot of errors. You need to replace the square brackets ( [ & ] ) with braces ( { & } ). Then you need to change void main to int main, and add a return 0 at the bottom. Then you can compile and run it yourself to get the output!
Aug 14 '07 #5

sicarie
Expert Mod 2.5K+
P: 4,677
Also, please have a look at the Posting Guidelines. I have deleted your other thread that contained the exact same question, please confine yourself to one thread per question, as well as using code tags and descriptive thread titles.

Thanks,

sicarie
Aug 14 '07 #6

P: 6
srry, dont understand what this is.


but why is erveryone saying to me that i must delete void and place int???
Aug 14 '07 #7

sicarie
Expert Mod 2.5K+
P: 4,677
srry, dont understand what this is.


but why is erveryone saying to me that i must delete void and place int???
http://www.delorie.com/djgpp/v2faq/faq22_25.html

Google searches lead to knowledge ;)

PS - did you read the Posting Guidelines?
Aug 14 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.