简体   繁体   中英

Unexpected output of int datatype

I created a simple program from the book let us c pg no.26 which is an example to illustrate and the code is somewhat like this

#include <stdio.h>

int main() {
char x,y;
int z;
x = 'a';
y = 'b';
z = x + y;
printf("%d", z);

return 0;
}

But the output i expected was the string ab (i know the z is in int but still that was the output i can think of) but instead the output was 195 which shocked me so please help me to figure this out in easy words.

Chars/letters are internally represented as numbers in terms of some protocols (eg, Ascii or Unicode). ASCII is a popular standard to represent the most common symbols and letters. Here is the ASCII table. This table tells all the common symbols/letters in ASCII are essentially a number between 0 and 255 (ASCII has two parts: 0 to 127 is the standard ASCII; the upper range of 128 to 255 is defined in Extended ASCII; many variants of extended ASCII are used).

To put it into the context of your code, here is what happened.

// The letter/char 'a' is internally saved as 97 in the memory
// The letter/char 'b' is internally saved as 98 in the memory
x = 'a'; // this will copy 97 to x
y = 'b'; // this will copy 98 to x
z = x +y ; // 97+98=195 -> z

If you want to print "ab", you must have two chars next to each other. Here is what you should do

char z[3];
z[0]='a'; //move 'a' or 97 to the first element of z (recall in C, the index is zero-based
z[1]='b';//move 'b' or 98 to the second element or z
z[2]=0;  //In C, a string is null-ended. That is, the last element must be a null (i.e.,0).

print("%s\n",z); // you will get "ab"

Alternatively, you can get "ab" in the following way based on the Ascii table:

char z[3];
z[0]=97; //move 97 to the first element of z, which is 'a' based on the ascii table
z[1]=98;//move 98 to the second element or z, which is 'b'
z[2]=0;  //In C, a string is null-ended. That is, the last element must be a null (i.e.,0).

print("%s\n",z); // you will get "ab"

Edit/comment:

Considering this comment:

"Chars are signed on x86, so the range is -128 ... 127 and not 0 ... 255 as you state ".

Note that nowhere did I mention that the char type in C has a range of 0 ... 255. I refer to [0 ... 255 ] only in the context of the ASCII standard.

You summed up 97 to 98, hence the 195.

Feeding a sum of two char in an int will promote those char to int then store the result.

Then if you want that to be printed as a string, you can printf("%s\\n", z); . Printing %d will interpret the variable as a decimal signed integer.

Don't print that as a string, because you don't know how far the first chars array terminator is.

Chars array in C, for many functions such as printf , don't end where its size ends , but where the terminator char ( 0x00 or 0 or '\\0' ) marks its end.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM