Created
August 2, 2014 20:40
-
-
Save dcorking/757256d5851f29fd7fb5 to your computer and use it in GitHub Desktop.
Typecasting int to char in printf() in C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <limits.h> | |
/* implements http://stackoverflow.com/questions/25079788/typecasting-int-to-char-in-printf-in-c */ | |
/* by discussedtree */ | |
/* Output is */ | |
/* Int size in bits is: 32 */ | |
/* Without typecasting, the integer at byte #0 is set to: 53200 */ | |
/* The integer at byte #0 is set to: -48 */ | |
int * int_pointer; | |
int main() | |
{ | |
int *int_pointer = malloc(10); | |
*int_pointer = 53200; | |
printf("Int size in bits is: %d \n ", (int) sizeof(int) * CHAR_BIT); | |
printf("Without typecasting, the integer at byte #0 is set to: %d \n ", *int_pointer); | |
printf("The integer at byte #0 is set to: %d \n", (char) *int_pointer); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment