C语言格式化Int64

经常使用,为方便查找特此记录.

int64_t数的输出:%lld ;
uint64_t数的输出:%llu ;
uint64_t数十六进制输出:%llx ;
uint64_t数八进制输出:%llo ;

int64_t

#if defined(_MSC_VER)		
		_snprintf_s(&buffer, SE_MAX_NUMSTR, SE_MAX_NUMSTR, "%I64d", val);
#else
#	ifdef __x86_64__
		snprintf(&buffer, SE_MAX_NUMSTR, "%ld", val);
#	elif __i386__
		snprintf(&buffer, SE_MAX_NUMSTR, "%lld", val);
#	endif
#endif

uint64_t

#if defined(_MSC_VER)		
		_snprintf_s(&buffer, SE_MAX_NUMSTR, SE_MAX_NUMSTR, "%I64u", val);
#else
#	ifdef __x86_64__
		snprintf(&buffer, SE_MAX_NUMSTR, "%lu", val);
#	elif __i386__
		snprintf(&buffer, SE_MAX_NUMSTR, "%llu", val);
#	endif
#endif

PostgreSQL 十六进制转换为整数

SELECT x'ffffffff'::integer,x'ffffffffffffffff'::bigint

补充

#include 
#include 
int main(int argc, char** argv) {
#ifdef _MSC_VER
#ifdef _WIN64
	fprintf(stdout, "size_t:%zu\n", sizeof(size_t));
	fprintf(stdout, "int:%zu\n", sizeof(int));
	fprintf(stdout, "long:%zu\n", sizeof(long));
	fprintf(stdout, "long int:%zu\n", sizeof(long int));
	fprintf(stdout, "long long int:%zu\n", sizeof(long long int));
#elif _WIN32
	fprintf(stdout, "size_t:%u\n", sizeof(size_t));
	fprintf(stdout, "int:%u\n", sizeof(int));
	fprintf(stdout, "long:%u\n", sizeof(long));
	fprintf(stdout, "long int:%u\n", sizeof(long int));
	fprintf(stdout, "long long int:%u\n", sizeof(long long int));
#endif
#else
#ifdef __x86_64__
	fprintf(stdout, "size_t:%lu\n", sizeof(size_t));
	fprintf(stdout, "int:%lu\n", sizeof(int));
	fprintf(stdout, "long:%lu\n", sizeof(long));
	fprintf(stdout, "long int:%lu\n", sizeof(long int));
	fprintf(stdout, "long long int:%lu\n", sizeof(long long int));
#elif __i386__
	fprintf(stdout, "size_t:%u\n", sizeof(size_t));
	fprintf(stdout, "int:%u\n", sizeof(int));
	fprintf(stdout, "long:%u\n", sizeof(long));
	fprintf(stdout, "long int:%u\n", sizeof(long int));
	fprintf(stdout, "long long int:%u\n", sizeof(long long int));
#endif
#endif;
	system("pause");
	return 0;
}

VC X64

size_t:8
int:4
long:4
long int:4
long long int:8

VC X86

size_t:4
int:4
long:4
long int:4
long long int:8

gcc x86_64

size_t:8
int:4
long:8
long int:8
long long int:8

gcc i386

size_t:4
int:4
long:4
long int:4
long long int:8

除了long long int固定为8字节外,其它类型都会发生变化
建议使用C99 stdint.h标准规范化程序

#include 
#include 
#include 
int main(int argc, char** argv) {
	fprintf(stdout, "C99 stdint.h\n");
	fprintf(stdout, "int8_t:%zu\n", sizeof(int8_t));
	fprintf(stdout, "int16_t:%zu\n", sizeof(int16_t));
	fprintf(stdout, "int32_t:%zu\n", sizeof(int32_t));
	fprintf(stdout, "int64_t:%zu\n", sizeof(int64_t));
	system("pause");
	return 0;
}
/*
int8_t:1
int16_t::2
int32_t:4
int64_t:8
*/

你可能感兴趣的:(C++)