Having simple c++ program like so:
#include <stdio.h> #include <limits.h> template <typename T> char BinVal(T SrcVal) { //short BitsCount = sizeof(T)*CHAR_BIT; short BitsCount = sizeof(T)*CHAR_BIT; short SeparatorCount = sizeof(T)*2-1; short SeparatorSize = CHAR_BIT/2+1; static char BinValStr[ BitsCount + SeparatorCount ] = {0}; printf("BitsCount: %d\n", BitsCount); printf("SeparatorCount: %d\n", SeparatorCount); printf("BinValStr size: %d\n", BitsCount + SeparatorCount); int i = 0; int j = 0; for ( i=BitsCount+SeparatorCount-1; i>=0; i-- ) { if( (j+1)%SeparatorSize == 0) { BinValStr[i] = ' '; } else { if( SrcVal&1 == 1 ) BinValStr[i] = '1'; else BinValStr[i] = '0'; SrcVal >>=1; } j++; } char y='o'; return y; //return BinValStr; //printf("\nStr: %s", BinValStr); } int main(){ short val = 0b0000'0100'0001'0110; //18 dec printf("\nStr: %c", BinVal(val) ); } i get "storage size of ‘BinValStr’ isn’t constant" error.
When i make the BinValStr var "normal", local everything is OK. Why does making the variable static gives me the error?