Is there such thing as a negative ASCII value ?

Dani AI

Generated

Short answer: ASCII itself is 7-bit only, values 0..127. There are no negative ASCII codes. The negatives you see come from how C treats char when it is a signed type and then promotes it to int for printf. Bytes with the high bit set (128..255) become negative when interpreted as signed char. To print byte values predictably, cast to unsigned char (or use an int) before printing.

Your loop also decrements while testing <= 'Z', so it keeps going until the char value wraps around. That wrap is undefined for signed types. Use an int for the loop counter and increment instead.

for (int ch = 'A'; ch <= 'Z'; ch++) {
    printf("value(%c) = %u\n", ch, (unsigned)ch);        /* prints 65..90 */
}

/* Always print bytes via unsigned char to avoid negatives */
unsigned char b = 200;
printf("byte = %u\n", (unsigned)b);                      /* prints 200 */

On the ">>" question: that is two characters, each with ASCII code 62. If you ever see -69, that is 187 when viewed as unsigned, which commonly maps to the single character "»" in many legacy code pages (e.g., Windows-1252). It can resemble a double greater-than. If you need the codes for each byte in a string, examine them separately and print as unsigned:

const unsigned char s[] = ">>";
printf("%u %u\n", s[0], s[1]);                           /* 62 62 */

Recommended Answers

All 8 Replies

Member Avatar for Member #27895

No. That's all there is to it, but the forum requires at least 10 characters in a message. :)

well my friend compile and run this tiny code and see what happens for yourself , i found this code from a book that am reading right now and modified it out of curiousty and BOOM !!! c assigns a negative ASCII value to some symbols , please somenoe explain to me if there is such thing as a negative ASSCII value . If the answer is NO why is c assighning a neg value to those symbols in this particular program . Thanx like always

#include <stdio.h>



int main()
{
char ch;


for (ch = 'A'; ch <= 'Z'; ch--)
printf("\aASCII value for %c is %d.\n", ch, ch);


system("PAUSE");
return 0;
}

The ascii character set assignsg the (256) numbers that can be represented by an unsigned char to display characters. The set runs from 0 - 255. No negatives.

C/C++ uses chars to store ascii values, though they are really just numbers. The trick is in how they are interpreted by certain functions (eg printf).

Your sample code seems to display -ve ascii values. However the printf is being asked to interpret unsigned char values as signed values.

I expect that in your system char == unsigned char.

The implicit conversion (in this case from signed to unsigned) is a 'feature' of the language. If you know about it, it can be very useful.

eg consider what the following snippet will display

for ( signed char x=1; x != 0; ++x )
{
cout << x << "," << (unsigned char)x << ", " << (char)x << " \n";
}

>c assigns a negative ASCII value to some symbols
No, C assigns a negative integral value to the variable and the system happens to have a printable representation for that value. It doesn't mean that it's ASCII.

>I expect that in your system char == unsigned char.
No, if that were the case the the loop would terminate after ch reached 0. This is because the value would roll around to the largest unsigned value for char. That value is larger than the ASCII value for 'Z', so the loop would break successfully. The fact that the OP is seeing negative values is an indicator that the default type for char is signed, not unsigned.

>If you know about it, it can be very useful.
Very useful for breaking things.

>for ( signed char x=1; x != 0; ++x )
This is undefined behavior. You're overflowing a signed integral type.

ohhhhh i c......... integral !! not ASCII value . many thnks to you narue , and to all of you who replied

Is there any symbol for which there is no ascii value and c prints a -ve garbage value for that. I am getting the value -69 wheneevr I am trying to print the ascii value of the symbol ">>". Please treat the symbol within quotes as a single symbol.I am sending the file test.txt containing the particular symbol.

Debojyoti

there are a lot of unprintable characters between 0 and 255. Look at any ascii chart such as this one and you see that printable chars start with decimal value 32 (a space) and ends in decimal value 126 the tilda ~. Values above and below those two values may or may not be pritable, depending on the code, or language, set.

So if you try to print one of those non-printable characters to a file in its binary form (which is probably what happened in the text file you posted) all you will most likely see in the file is a funny-looking square when displayed with Notepad. Other text editors may display them as some other symbol or not at all.

There is nothing called a negative ASCII value. Every symbol should have an ASCII value.

I think you wanted to print the character variable using a %d notation, for which you received -69.

As already mentioned in many messages below you should have your system char as unsigned char. But in your case I think its a signed one. Hence after the value 0f 127 it goes in the negative way.

Thus it is not a garbage value also but a signed representation of your input.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.