How to check an integer variable is signed in C

1 Answer

0 votes
#include <stdio.h> 
  
#define ISVARSIGNED(V) ((V)-1 < 0 || -(V)-1 < 0)

int main() 
{ 
    signed int a = 2938, b = -382; // == int a = 2938, b = -382;
    
    printf("%s\n", ISVARSIGNED(a) ? "yes": "no");
    printf("%s\n", ISVARSIGNED(b) ? "yes": "no");
    
    unsigned int c = 90298, d;
    
    printf("%s\n", ISVARSIGNED(c) ? "yes": "no");
  
    return 0; 
}



/*
run:

yes
yes
no

*/

 



answered Apr 2, 2024 by avibootz

Related questions

1 answer 150 views
1 answer 119 views
2 answers 201 views
1 answer 169 views
1 answer 168 views
1 answer 104 views
...