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

character code points versus plain integers

P: 14
if i do this:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. int main()
  4.  
  5. {
  6.   char number1 = '9' , number2 = '1' , answer ;
  7.   answer = '9' + '1'
  8.  
  9.   cout << answer << endl ;
  10.   cin.get()
  11. }
then the result will be: :



and if i do this :
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. int main()
  4.  
  5. {
  6.   char number1 = 9 , number2 = 1 , answer ;
  7.   answer = 9 + 1
  8.  
  9.   cout << answer << endl ;
  10.   cin.get()
  11. }
whats the result now?
Aug 14 '07 #1
Share this Question
Share on Google+
49 Replies


Expert 10K+
P: 11,448
'1' is just another notation for the Latin 1 (formerly known as ASCII) code for the
code point 49. '9' has code point value 57. So basically you're adding 49+57.

kind regards,

Jos
Aug 14 '07 #2

sicarie
Expert Mod 2.5K+
P: 4,677
'1' is just another notation for the Latin 1 (formerly known as ASCII) code for the
code point 49. '9' has code point value 57. So basically you're adding 49+57.

kind regards,

Jos
mcfaker, I mean, dorkz, did you try putting it into a compiler?

And please use code tags in the future.
Aug 14 '07 #3

P: 14
thanks , but you mean that in the first example with the apostrofs, the result will be : 49 + 57 = 106, and 106 is character 'j' ??? do you mean that?
Aug 14 '07 #4

Expert 10K+
P: 11,448
thanks , but you mean that in the first example with the apostrofs, the result will be : 49 + 57 = 106, and 106 is character 'j' ??? do you mean that?
I don't know what the code point value of a lower case 'j' is, but if it is 106, then yes,
that's what I mean.

kind regards,

Jos
Aug 14 '07 #5

P: 14
yes it is 'j'


but , by the second example , the example without the apostrofs , what is the result of this? 10?
Aug 14 '07 #6

P: 14
hello? coolpeople coming to help me.
Aug 14 '07 #7

Expert 10K+
P: 11,448
hello? coolpeople coming to help me.
You do know how to do simple additions do you?

kind regards,

Jos
Aug 14 '07 #8

sicarie
Expert Mod 2.5K+
P: 4,677
You do know how to do simple additions do you?

kind regards,

Jos
And if you don't want to do addition, see post #3.
Aug 14 '07 #9

P: 14
yes i know, but come on answer please, cause ur soo smart,please answer that question. thank you , thank you , thank you
Aug 14 '07 #10

P: 14
look 9 + 1 without apostrofs is 10 anyhow?
Aug 14 '07 #11

P: 14
please answer wirhout apostrofs, will it be 10??
Aug 14 '07 #12

sicarie
Expert Mod 2.5K+
P: 4,677
dorkz-

Please check your PM's - they are in the top right of the page.

Thanks,

sicarie
Aug 14 '07 #13

P: 14
im in shock! but please answer, then i stop answering?ok?
Aug 14 '07 #14

Expert 10K+
P: 11,448
im in shock! but please answer, then i stop answering?ok?
Everything is answered already. '1'+'9'= 106; 1+9= 10.

kind regards,

Jos
Aug 14 '07 #15

P: 6
yes, yes fiendly, thank you!
Aug 14 '07 #16

P: 25
hey, if i start this program

#include < iostream >

int main()
{
int numberone = '1' , numbertwo = '9' , numberthree ;
numberthree = numberone + numbertwo ;

cout << numberthree ;
cin.get() ;
}

can i put these '1' and '9' in integers???

and something else: if i can put it into integers, the result would be 106?

thanks for answering
please ban me again
Aug 14 '07 #17

P: 25
no i mean dont ban me!!!!
Aug 14 '07 #18

sicarie
Expert Mod 2.5K+
P: 4,677
no i mean dont ban me!!!!
I asked you to do something very simple. That was read the Posting Guidelines and follow them.

You did the same things I warned you against: double-posting, not using code tags, etc..., and now you're doing them again.

Read the Posting Guidelines. Follow them and you won't get warned, banned, or spoken harshly to.
Aug 14 '07 #19

sicarie
Expert Mod 2.5K+
P: 4,677
Not only that, but Jos answered you, and you could always put it in a compiler and figure it out.
Aug 14 '07 #20

weaknessforcats
Expert Mod 5K+
P: 9,197
Of course it's 106. That's what your program displays.

Now, what is your question?
Aug 14 '07 #21

P: 25
ok i read it, but where is it?
Aug 14 '07 #22

sicarie
Expert Mod 2.5K+
P: 4,677
dorkz-

Please check your PM's - they are in the top right of the page.

Thanks,

sicarie

Funny how that reading thing helps.

So, since you don't want to use that account:

The Posting Guidelines yet again.
Aug 14 '07 #23

P: 25
but the next quetion is, : but can i set characters in integers?
Aug 14 '07 #24

sicarie
Expert Mod 2.5K+
P: 4,677
but the next quetion is, : but can i set characters in integers?
Why don't you take a minute to read the guidelines before blindly posting again?
Aug 14 '07 #25

Expert 10K+
P: 11,448
but the next quetion is, : but can i set characters in integers?
Yes you can; read up on integer promotion and try to figure out what the result
'll be for the following:

Expand|Select|Wrap|Line Numbers
  1. int i= '9'+'1'-'0'-'0';
  2.  
Compile it, test it, play with it a bit and please try to understand it. Asking all
sorts of questions without trying to find the answer is not the way to acquire
any knowledge.

kind regards,

Jos
Aug 14 '07 #26

P: 25
but when i compile this , its coming with errors
Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2.  
  3.  
  4. int _tmain(int argc, _TCHAR* argv[])
  5. {
  6. int number1 = '9' , number2 = '1' , answer ;
  7. answer = '9' + '1' ;
  8.  
  9. cout << answer;
  10. cin.get(); 
  11.  
  12. }
  13.  
error: 1>------ Build started: Project: er, Configuration: Debug Win32 ------
1>Compiling...
1>er.cpp
1>c:\documents and settings\eigenaar\mijn documenten\visual studio 2005\projects\er\er\er.cpp(12) : error C2065: 'cout' : undeclared identifier
1>c:\documents and settings\eigenaar\mijn documenten\visual studio 2005\projects\er\er\er.cpp(13) : error C2065: 'cin' : undeclared identifier
1>c:\documents and settings\eigenaar\mijn documenten\visual studio 2005\projects\er\er\er.cpp(13) : error C2228: left of '.get' must have class/struct/union
1> type is ''unknown-type''
1>Build log was saved at "file://c:\Documents and Settings\Eigenaar\Mijn documenten\Visual Studio 2005\Projects\er\er\Debug\BuildLog.htm"
1>er - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========



dont understand what to change!
Aug 14 '07 #27

sicarie
Expert Mod 2.5K+
P: 4,677
Funny how that reading thing helps.

So, since you don't want to use that account:

The Posting Guidelines yet again.
READ THE PART ABOUT CODE TAGS AND HOW THEY WORK.
Aug 14 '07 #28

sicarie
Expert Mod 2.5K+
P: 4,677
Comments in code

Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. // you need libraries to handle cout and cin
  3.  #include <iostream> 
  4.  using namspace std;
  5.  
  6.  
  7. int _tmain(int argc, _TCHAR* argv[]) // looks like an MS thing, but whatever
  8. {
  9.    int number1 = '9' , number2 = '1' , answer ;
  10.    answer = '9' + '1' ; // why do you not use number1 and number 2?
  11.    // if you don't use them, why bother declaring?
  12.    // it doesn't matter, your answer is the same anyway.
  13.  
  14.    cout << answer;
  15.    cin.get(); // can't remember how this behaves, but more importantly, I'm not sure why it's here.
  16.  
  17.    // you need this, you declare int main
  18.    return 0;
  19. }
  20.  
Aug 14 '07 #29

P: 25
dont see anything about tags ; Do NOT PM questions to individual Experts, Moderators or Administrators
Before you post your question
Things that are generally unacceptable
Use a Good Thread Title
How to ask a question
Posting Homework or Coursework Questions and Answers
Do Not Double Post Your Questions
Once Your Question Has Been Answered
While you are in the forum
How to Respond to a Question


Codes of Conduct
Code of Conduct - For Moderators and Administrators


Guideline to Moderators
This FAQ is meant for those with moderation rights to give them an idea of when it is appropriate to take any given action. It is put in the public domain so that users of the site may see the advice given to our moderators and may post appropriately in light of this information.

If you are a user and feel that a moderator has overstepped their responsibilities in some way please contact an Administrator stating in a clear and calm manner what your complaint is.

Editing, Deleting, Moving, Merging and Splitting Threads
Editing a Thread
Editing a Post
Deleting a Thread
Deleting a Post
Moving a Thread
Merging Two Threads
Splitting A Thread
Response to Class Assignments
Standard Responses to Various Situations
When to ban a user, how long to ban them for
Aug 14 '07 #30

sicarie
Expert Mod 2.5K+
P: 4,677
And once again, this is where reading helps.

How to ask a question

How to ask a question
Please follow these guidelines when posting questions as submitting clear and concise questions allows those reading to understand your problem and respond more easily.

* Post your question in a relevant forum
* Make sure you are not posting in an Articles section
* Give the relevant Platform, OS and Version information
o In the C/C++ forum this might be Intel/Windows/MSVC++ 6.0 or StrongArm/ThreadX/Greenhills C/C++
o In Access it might just be the version of Access you are using 97, 2003 etc.
* Give as much detail as possible When you post a question or problem, express the situation clearly and concisely and include all relevant information, code used, data used, result expected, result achieved and any error codes or messages that you get as a result.
* Use Clear English to write your question in if possible, try to avoid using abbreviations
* Do not use leet speak or text speak, they are not Clear English
* Make use of the available formatting tags in the forum Use CODE tags around your code as appropriate:
o
Expand|Select|Wrap|Line Numbers
  1. ..code goes here..
o
Expand|Select|Wrap|Line Numbers
  1.  ..php code goes here.. 
o
Expand|Select|Wrap|Line Numbers
  1.  ..C like code goes here.. 
o [code=html] ..html code goes here.. [/html]
This will preserve white space and use a mono-spaced font making the code easier to read.
* Do not ask people to reply by email or follow up answers with a PM
* Please don't say that a problem is urgent if it's not. If your problem is time sensitive, please give specific time frames e.g. In 3 days, within 24 hours.
* Please try to read your own post after you've posted it. If you can't make any sense of it, it's a fair bet that our experts will struggle and waste time just trying to understand what you're trying to say. Also, if you get bored when you're half-way through reading it because it rambles on endlessly, how will our experts fare?
* If you wish to post a question do not post it in a discussion created by someone else unless it is about exactly the same problem. Please start a new discussion.

* Full list of Code Tag Supported Languages
* Access Forum Specific Guidelines
Be sure you actually click on the link - because I'm posting this in a forum, the code tags are being rendered - the link has them how they are supposed to be.
Aug 14 '07 #31

P: 25
man, i'm soo dutch , do you know that? i dont understand code tags and all that other stuff.
Aug 14 '07 #32

sicarie
Expert Mod 2.5K+
P: 4,677
man, i'm soo dutch , do you know that? i dont understand code tags and all that other stuff.
Dude, I'm sorry, but if you can't read that an understand (or even try to use them), and haven't noticed that it's also on the box to the right of you when you create, edit, or reply to a thread, I can't help you. Maybe one of the other mods can, but I've been trying all day, and it almost seems like you're trying to be difficult.

Good luck
Aug 14 '07 #33

Expert 10K+
P: 11,448
man, i'm soo dutch , do you know that? i dont understand code tags and all that other stuff.
*ahem* I have the Dutch nationality. How about *reading* a bit? What actually
do you see when you have all that information right in front of you? Just a blur
that you wish to ignore? Please *read* and try to *understand* what it's all
about. I know you're from the cut-and-paste generation but you can at least
attempt to understand *what* to cut-and-paste.

kind regards,

Jos
Aug 14 '07 #34

P: 25
now, i've waited , now i want to ask a question again,
'6' is a character, 6 is an integer.
integers like 2,4,5,..... ,we putt them in int: integer
characters 'A', '4','d',.......we put them in char : character

that means ; char for characters
int for integers
(if string exist: string for letters)

we cannot put characters in int
we cannot put integers in char,
but some people here saying that we can put characters in int???????
how???????????
Aug 14 '07 #35

Expert 10K+
P: 11,448
we cannot put characters in int
we cannot put integers in char,
but some people here saying that we can put characters in int???????
how???????????
By putting chars in ints and vice versa because it can be done. If you had read
at least an introduction text you could and would have known this and what the
restrictions are. Stop asking silly questions over and over again. Sicarie was
patient with you; I am not. I don't want these forums polluted by your illiteracy.

Jos
Aug 14 '07 #36

P: 25
ok, i'm reading your rulez, but PM , theres writed that i dont must pm (or something) experts, do they mean : dont ask question every minute?
Aug 14 '07 #37

Expert 10K+
P: 11,448
ok, i'm reading your rulez, but PM , theres writed that i dont must pm (or something) experts, do they mean : dont ask question every minute?
PM == Private Message. I am officially warning you now: I don't want to see
any question from you anymore before you can more or less explain what chars
and ints are and what 'A' and 65 mean. I also want you to explain more or less
why ints can be assigned to chars and vice versa.

Now go and read a book; if I see you here before I'll ban you. You have been warned.

Jos
Aug 14 '07 #38

P: 25
i've readed everything only what i dont understand : Make use of the available formatting tags in the forum Use CODE tags around your code as appropriate:
Expand|Select|Wrap|Line Numbers
  1. ..code goes here..
Expand|Select|Wrap|Line Numbers
  1.  ..php code goes here.. 
Expand|Select|Wrap|Line Numbers
  1.  ..C like code goes here.. 
[code=html] ..html code goes here.. [/html]


heeyyyy!!! i know everything about these characters and int , but i just dont know why you can put an caracter into an integer!!
Aug 14 '07 #39

P: 25
Expand|Select|Wrap|Line Numbers
  1.  char name, exist, dont
  2.  
Aug 14 '07 #40

P: 25
hey, i know how to use it!!
Aug 14 '07 #41

Expert 10K+
P: 11,448
hey, i know how to use it!!
This is two; one more and you're out.

Jos
Aug 14 '07 #42

P: 25
ok, here is the explaning of char; is a character with two apostrofs like ' ' or 'e' or '4'

integer is not a floating point number its like this 1, 2,3....
unsigned int, long int, unsigned long are too types of int but they have more space for numbers

then we have float , for numbers with . like 4,3854535
a type of float is too a double , its bether, and larger


that was the expalning you wanted.
now i can ask again questions??
Aug 14 '07 #43

P: 25
i really dont understand why this program dont work

// er.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>


int _tmain(int argc, _TCHAR* argv[])
{
int number1 = '9' , number2 = '1' , answer ;
answer = '9' + '1' ;

cout << answer;

cin.get () ;


}
Aug 14 '07 #44

Expert 10K+
P: 11,448
that was the expalning you wanted.
now i can ask again questions??
Sure; just behave like a normal human being and all will be fine. First *read*
before you want to ask a question though; reading helps you understand before
you think you have to ask a question; most likely you have found the answer
to your question yourself already.

kind regards,

Jos
Aug 14 '07 #45

P: 25
ok thanks , i stop asking for today
Aug 14 '07 #46

Expert 10K+
P: 11,448
i really dont understand why this program dont work

// er.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>


int _tmain(int argc, _TCHAR* argv[])
{
int number1 = '9' , number2 = '1' , answer ;
answer = '9' + '1' ;

cout << answer;

cin.get () ;


}
what is that _tmain and _TCHAR stuff all about? A proper C/C++ program needs
an int main(int argc, char* argv[]) function.

Also, you should specify the "don't work" part. Most of the repliers overhere are
not psychic. On top of that it should be "doesn't work" (singular form). This is an
international forum and non-native English speakers could be the folks that
could supply you with the best answers if only they could read your question.

kind regards,

Jos
Aug 14 '07 #47

TRScheel
Expert 100+
P: 638
After reading this thread... I am confused, but I think I get your problem.

You expect:

Expand|Select|Wrap|Line Numbers
  1. cout << '9' + '1';
to produce the output:

Expand|Select|Wrap|Line Numbers
  1. 10
but instead you get a much larger number or a letter. This is because when you surround a number or character in ' ' it becomes a char, and its integer value is different then its char value.

If you wrote:

Expand|Select|Wrap|Line Numbers
  1. cout << 9 + 1;
You would get the desired output.


PLEASE NOTE
Expand|Select|Wrap|Line Numbers
  1. '9' != 9
  2. '1' != 1
  3.  
(In case you do not know, != means does not equal)
Aug 14 '07 #48

Banfa
Expert Mod 5K+
P: 8,916
what is that _tmain and _TCHAR stuff all about? A proper C/C++ program needs
an int main(int argc, char* argv[]) function.
That is Microssofts newest version of main and char (or w_char) I have no idea why they feel the need to do this, especially as _tmain is just #defined to main in their headers.
Aug 15 '07 #49

Banfa
Expert Mod 5K+
P: 8,916
ok, here is the explaning of char; is a character with two apostrofs like ' ' or 'e' or '4'
This is not correct. A char is an integer type often it has 8 bits although this is platform dependent and has the range -128 - 127 (or 0 - 255 if char is unsigned on the platform). 'e' or '4' are character constants and have type char in C++ or int in C.

integer is not a floating point number its like this 1, 2,3....
unsigned int, long int, unsigned long are too types of int but they have more space for numbers

then we have float , for numbers with . like 4,3854535
a type of float is too a double , its bether, and larger
You have made similar mistakes here int is a variable type where as 1 is an integer constant and the same for float and 4.38.



I think you are missing another point which is that the computer does not understand or deal with characters like e (or formally the character constant 'e'). Your computer only deals with numbers (and in fact only deals with the numbers 0 and 1 which it uses long strings of to represent larger numbers). 'e' is just the way the computer outputs the number 101 to the screen to make it easier for humans to read when it is instructed to treat it as a character from the execution character set.

When you type 'e' into a program you are really using a shorthand for

'use the value that represents the letter e in the execution character set'

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main(int argc, char **argp)
  6. {
  7.     cout << static_cast<char>(101) << endl;
  8.  
  9.     return 0;
  10. }
  11.  
in some ways the C standard library function demonstrates this better than cout because it doesn't automatically select what it thinks is the correct interpretation of the data is but lets you specify how to output the data so

Expand|Select|Wrap|Line Numbers
  1. #include <cstdio>
  2.  
  3. int main(int argc, char **argp)
  4. {
  5.     char c1 = 101;
  6.     char c2    = 'e';
  7.     int i1 = 101;
  8.     int i2 = 'e';
  9.  
  10.     printf("%c == %d\n", c1, c1);
  11.     printf("%c == %d\n", c2, c2);
  12.     printf("%c == %d\n", i1, i1);
  13.     printf("%c == %d\n", i2, i2);
  14.  
  15.     return 0;
  16. }
  17.  
of course this probably only helps if you are familar with the printf function.
Aug 15 '07 #50

Post your reply

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