Skip to content

Instantly share code, notes, and snippets.

@alberthdev
Created March 24, 2015 01:35
Show Gist options
  • Save alberthdev/d02279e32865139f743c to your computer and use it in GitHub Desktop.
Save alberthdev/d02279e32865139f743c to your computer and use it in GitHub Desktop.
C uint8_t and uint32_t signedness check
// NOTE: This file should NOT compile
// If it does, that means uint8_t and uint32_t are already defined without an include file...
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
// Test if char and int are signed or unsigned
int main() {
// Char test
printf("Is uint8_t signed? ");
if ((uint8_t)-1 < 0)
printf("YES, char is signed\n");
else
printf("NO, char is unsigned\n");
// Int test
printf("Is uint32_t signed? ");
if ((uint32_t)-1 < 0)
printf("YES, int is signed\n");
else
printf("NO, int is unsigned\n");
// Print limits
printf("CHAR_MIN is %i\n", CHAR_MIN);
printf("INT_MIN is %i\n", INT_MIN);
exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment