Skip to content

Instantly share code, notes, and snippets.

@alberthdev
Last active August 29, 2015 14:17
Show Gist options
  • Save alberthdev/c81596297efb6ad89d09 to your computer and use it in GitHub Desktop.
Save alberthdev/c81596297efb6ad89d09 to your computer and use it in GitHub Desktop.
C Signedness Check
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
// Test if char and int are signed or unsigned
int main() {
// Char test
printf("Is char signed? ");
if ((char)-1 < 0)
printf("YES, char is signed\n");
else
printf("NO, char is unsigned\n");
// Int test
printf("Is int signed? ");
if ((int)-1 < 0)
printf("YES, int is signed\n");
else
printf("NO, int is unsigned\n");
// Print limits
// Negative values generally indicate signed-ness in type
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