Skip to content

Instantly share code, notes, and snippets.

@dcorking
Created August 2, 2014 20:40
Show Gist options
  • Save dcorking/757256d5851f29fd7fb5 to your computer and use it in GitHub Desktop.
Save dcorking/757256d5851f29fd7fb5 to your computer and use it in GitHub Desktop.
Typecasting int to char in printf() in C
#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