The Open Source Swiss Army Knife

/programmingToolBox/comp_orgI/
/programmingToolBox/comp_orgI/ + sub-categories
http://www.sirfsup.com/
web directory content
    
      

Not logged in
Chat Register Login
return to:  http:/www.sirfsup.com      /programmingToolBox   /comp_orgI 
Permalink: wraparound.c
Title: program showing that C stores chars as signed ints
article options : please login   |  raw source view  

/* this should cause the wrap-around effect but in fact it prints out 80000  */
/* but this is in fact causing not that effet at all but the correct number is being printed */
#include <limits.h>
int main() {
	unsigned int y;
	char r;
	unsigned char x;
	y = 30000 + 50000;
	printf ("%d\n", y);
	y = 300000 + 500000; /* 300,000 plust 500,000 */
	printf ("800,000 = %d\n", y);
	y = 3000000 + 5000000; /* 300,000 plust 500,000 */
	printf ("8,000,000 = %d\n", y);
	y = 30000000 + 50000000; /* 300,000 plust 500,000 */
	printf ("80,000,000 = %d\n", y);
	printf("int_max (prints now 2+ billion)= %d\n",INT_MAX);
	printf("char_max (prints now 127)= %d\n",CHAR_MAX);
	r = 'a' + 'z';
	printf ("char r = 'a' + 'z' ---> %d\n", r);
	printf ("219 = 11011011\n", r);
	printf ("the first bit is the sign, 1, and the remaining 7 (1011011) = 64+16+8+2+1 = 91in dec\n");
	printf ("219 = 11011011 and -37 = 11011011 have the same binary representation but I can't convert successfully 11011011 to binary\n", r);
	printf ("char are signed by default then, as this datatype was said to be char whereas below, it was deliberately said to be unsigned char\n");
	printf ("char a  ---> %d\n", 'a');
	printf ("char z  ---> %d\n", 'z');
	x = 'a' + 'z';
	printf ("unsigned char x = 'a' + 'z' ---> %d\n", x);
	/* a in dec is = 97 =  */
}

Leave a Reply
Your Name:     anonymous
Your Email:
Website:  
Comments:

The author will be notified of your reply.
return to top