C++ ASCI int values


 
Thread Tools Search this Thread
Top Forums Programming C++ ASCI int values
# 1  
Old 09-30-2009
C++ ASCI int values

Hi All,

I'm currently fiddling about trying to learn C++ and wrote a little program that outputs the ASCI values for numbers 0-255 but it's got a problem...

For the numbers 255 thru 128 it shows a negative number. For numbers 127-0 (my loop decrements) it shows the correct numerical value...

Here's my loop:

Code:
	for (c=255; c>0; c--)
	{
		// output the value of c
		printf ("%d : ",c);
		// make variable chChar the value of c
		chChar = c;
		// output asci value of chChar
		cout << chChar << " : " << (int)chChar << endl;
	}

I'm I hitting an overflow or something?

Many thanks Smilie
# 2  
Old 09-30-2009
Hi,
By default the char would be a signed char.
That is the reason you are getting negative and positive values.
Please correct it by replacing - "char" with "unsigned char;"

Hope it resolves your problem.

Have a Good Day !!!
# 3  
Old 09-30-2009
Thanks GaneshCPUX that solved the problem Smilie

...Now I have to get my head around why it fixed the problem Smilie


EDIT: Ahh yes of course, a signed char has 1 byte - an unsigned has that extra bit to toggle hense can go over 127 Smilie
# 4  
Old 09-30-2009
Nope. Both the signed and the unsigned char type are 1 Byte (8 bits) in size. The difference is that for a signed type, the MSB (most significant bit) is the sign indicator. So an unsigned char can go from 0 (0000 0000) to 255 (1111 1111), while a signed goes from 0 (0000 0000) to 127 (0111 1111) and -128 (1000 0000) to -1 (1111 1111)
# 5  
Old 09-30-2009
Quote:
Originally Posted by pludi
Nope. Both the signed and the unsigned char type are 1 Byte (8 bits) in size. The difference is that for a signed type, the MSB (most significant bit) is the sign indicator. So an unsigned char can go from 0 (0000 0000) to 255 (1111 1111), while a signed goes from 0 (0000 0000) to 127 (0111 1111) and -128 (1000 0000) to -1 (1111 1111)
Ahh, yes. I think I've got it. Both have the same number of bits available but the signed char has the MSB reserved for its sign indicator.

It's good to get the basics down early on Smilie

Thanks for your help Smilie
# 6  
Old 09-30-2009
The C99 standard (ISO/IEC 9899:1999) only requires the range -127 (SCHAR_MIN) to
+127 (SCHAR_MAX) for objects of type signed char. However -128 is valid for 8-bit
signed chars if 2s complement representation is used (which it almost always is).

Note also that there are actually three char types (See C99, 6.2.5.14 and 6.2.5.15)
The three types char, signed char, and unsigned char are collectively called
the character types. The implementation shall define char to have the same range,
representation, and behavior as either signed char or unsigned char.
# 7  
Old 09-30-2009
Quote:
Originally Posted by fpmurphy
The three types char, signed char, and unsigned char are collectively called the character types. The implementation shall define char to have the same range, representation, and behavior as either signed char or unsigned char.
Which is why I always declare the type explicitly.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

'int air_date' '%'?

int air_date='20100103'; //2010 - Jan - 03 /* My goal here is to subtract a day. */ int day = air_date % 100; //?????? Is this right? //Are there any functions time/date for this type of date format? :cool: (7 Replies)
Discussion started by: sepoto
7 Replies

2. Programming

Handle int listen(int sockfd, int backlog) in TCP

Hi, from the manual listen(2): listen for connections on socket - Linux man page It has a parameter called backlog and it limits the maximum length of queue of pending list. If I set backlog to 128, is it means no more than 128 packets can be handled by server? If I have three... (3 Replies)
Discussion started by: sehang
3 Replies

3. AIX

Disabling an ASCI terminal in AIX versions 3 and 4

Hi, I tried to do some research on this subject, but got nothing conclusive. I have the following need: I have different servers with AIX versions 3.2.5 through 4.3.2. Some of them have two ASCI terminals connected. I have a shell script that is executed by a user on the main console... (2 Replies)
Discussion started by: andrei_r20
2 Replies

4. Shell Programming and Scripting

From string to int ?

hello guys i m new to shell scripting and can't find out why this structure is not right I m guessing this happens because $LINESUM is a string . so how can i do this ? i want my script to do so many loops as the number of the lines of one custom file. #!/bin/bash echo give me path name... (5 Replies)
Discussion started by: xamxam
5 Replies

5. Programming

int *ptr + max possible value

From reading my C book, Im aware that the integers have a maximum value which depends on what type of processor you are using (since they use 16-bit or 32-bit instructions). Now I know pointers are very flexible, since they can reference anything, but in the case of integer pointers, can they... (4 Replies)
Discussion started by: JamesGoh
4 Replies

6. Programming

to convert int to hex

Hi, Can you help me in converting int value to hex in a single command. Thanks (8 Replies)
Discussion started by: naan
8 Replies

7. UNIX for Dummies Questions & Answers

int open(const char *pathname, int flags, mode_t mode) doubt...

hello everybody! I want to create a file with permissions for read, write, and execute to everybody using C, so I write this code: #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int main(){ int fileDescriptor; fileDescriptor =... (2 Replies)
Discussion started by: csnmgeek
2 Replies

8. Shell Programming and Scripting

how to convert a string to int

Hi, i want to read a text file whose content(single line) will be a number like 1 or 2 or 3 ..... what i want to do is to read the file and increment the content of the file, using unix scripting. Regards, Senthil Kumar Siddhan. (2 Replies)
Discussion started by: senthilk615
2 Replies

9. Programming

difference between int ** func() and int *& func()

What is the difference between int** func() and int*& func(). Can you please explain it with suitable example. Thanks, Devesh. (1 Reply)
Discussion started by: devesh
1 Replies

10. Programming

Unsigned int

How can I store and/or print() a number that is larger than 4 294 967 295 in C? is int64_t or u_int64_t what I need ? if, so how can I printf it to stdout? (2 Replies)
Discussion started by: nimnod
2 Replies
Login or Register to Ask a Question