Last active
August 29, 2015 14:17
-
-
Save alberthdev/c81596297efb6ad89d09 to your computer and use it in GitHub Desktop.
C Signedness Check
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 <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