这个问题以前曾问过,但我仍然感到困惑。
我知道
unsigned int a = -1;
会是UINT_MAX。但这并不是因为-1的所有位元都设置好了。C11说
如果新类型是无符号的,则通过重复添加或减去比在新类型中表示的最大值多一个值来转换值,直到该值位于新类型的范围内为止。
让我们假设UINT_MAX是100 (我知道它应该大于2^16-1,但现在让我们忽略这一点)
unsigned int a = -1; // will be
unsigned int a = -1 + UINT_MAX + 1; // 100 = UINT_MAX
标准只说UINT_MAX >= 2^
编辑:更改了USHRT_MAX的值,因为它不符合,如注释所示。
假设您有一个花哨的编译器,其中其整数类型限制(在Lims.h中定义)是:
#define INT_MAX 2147483647 /* Typical 32-bit system value */
#define USHRT_MAX 2147483647 /* ***Edited***, original question had 2000000000 */
在我的应用程序中,我有以下代码:
unsigned short a = 1500000000;
unsigned short b = 1500000000;
un
每当我尝试通过TCP套接字发送负数时,当我打印接收到的内容时,它显示为"4.29497e+09“。我要做的就是:
int i = -8;
int temp = htonl(i);
write(sock,&temp,4);
在服务器上:
int temp;
read(sock, &temp,4);
int read = ntohl(temp);
cout << read << endl;
如果有人能帮上忙,我将不胜感激。
我有个问题。我的节目里有台词:
sprintf(buff,"Nieznany znak: %d",(char)va_arg(x, const char)); break;
为什么在编译后我有一个错误:
error.c: In function 'error':
error.c:30:46: warning: 'char' is promoted to 'int' when passed through '...' [e
nabled by default]
case 0xfe: sprintf(buff,
下面是在VS2005中编译的警告级别4的示例:
int main(int argc, char *argv[])
{
short s = 2;
short t = 3;
t *= s; // warning C4244: '*=' : conversion from 'int' to 'short', possible loss of data
t = t * s;
}
在我看来,这两条线上都不应该有警告。
*=是否出于某种原因创建了对int的隐式转换?
编辑:
似乎第二行(以及在VS2008中)缺乏警告才
我的工作站有128 My的内存。我不能分配占用(连续)内存超过~16 an的数组。但是我可以分配多个数组,每个数组大约需要15 can。 示例代码: #include <stdlib.h>
#include <iostream>
using namespace std;
int main(int argc, char **argv)
{
int MM = 1000000;
int NN = 2200; // 2000 is okay, used ~16GB memory; 2200 produces Segmentation fault
double* testMe
我正在做一个项目,在这个项目中,RAM中的数据是非常重要的。如果是大端,我需要把它转换成小端点。我编写的这个简短的程序是否可靠地决定了任何Linux系统的性能?
此外,是什么决定了存储在RAM中的数据的独立性?
#include <stdio.h>
#include <stdint.h>
int main ()
{
// This program will determine the endianness
// of your system and print the result in human
// readable form to st
代码如下:目标是判断数组中所有成员的和是正还是负。我知道无符号int是一个错误,因为我想看看它会打印什么。所以问题是为什么它打印负数,我的意思是,将负整数赋值给无符号整数的结果是什么。
#include <stdio.h>
#define ARR_SIZE 5
int main() {
int array[ARR_SIZE] = {1,-2,3,4,-5};
unsigned sum;
int i;
for ( i = 0, sum=0; i < ARR_SIZE; i++ )
sum += array[i];
if( sum > -1 ) printf
我有一个非常恼人的问题。这是我正在使用的buffer结构
typedef struct BufferDescriptor {
char * ca_head ; /* pointer to the beginning of character array (character buffer) */
int capacity ; /* current dynamic memory size (in bytes) allocated to character buffer */
char inc_factor; /* character arra
我正在尝试将一个整数写入二进制文件,然后读取相同的内容。但是,我的程序读取的数字与写入的数字不同。我做错了什么?
unsigned short int numToWrite = 2079;
// Write to output
FILE *write_ptr;
write_ptr = fopen("test.bin","wb"); // w for write, b for binary
printf("numToWrite: %d\n", *(&numToWrite));
fwrite(&
可能重复:
1.
main()
{
if(-1<(unsigned char)1)
printf("-1 is less than (unsigned char)1:ANSI semantics");
else
printf("-1 NOT less than (unsigned char)1:K&R semantics");
}
2.
int array[] = {23,41,12,24,52,11};
#define TOTAL_ELEMENTS (sizeof(array)/sizeof(array[0])
我不知道当我使用未签名的字符时,我什么时候要担心溢出。这个案件是明确的:
uint8_t a = 3;
uint8_t b = 6;
uint8_t c = a - b; // c is 253
然而,这里发生了什么:
float d = a - b; // d is -3
在减法之前,a和被转换成浮点数吗?
或者在这种情况下:
float e = (a - b) + (a - c);
这三个变量都转换成浮点数了吗?
是否有可能发生溢出,即使分配给的变量是浮点数?如果e是浮点、整数或其他任何东西,规则是相同的吗?
同样,在这种情况下会发生什么:
int a = std::abs(a - b)
我想把两个数字相乘,我知道我的数字总是正数,然后:
unsigned int mulPositiveNumbers(unsigned int a ,unsigned int b)
{
assert(a > 0);
assert(b > 0);
return (a*b);
}
现在,我使用assert告诉自己“给定的数字总是正数”。
但是当我运行的时候:
int main()
{
unsigned int res = mulPositiveNumbers(-4,3);
// more stuff goes here
}
即使我使用
如果我编译了以下代码
size_t a = -1;
使用带有W4选项的MSVC,我得到
warning C4245: 'initializing': conversion from 'int' to 'size_t', signed/unsigned mismatch
我也不能百分之百肯定-1在所有平台上都是0xFFFFFFFF。-1位表示是否由标准定义?
其他选择包括:
size_t a = std::numeric_limits<size_t>::max();
size_t a = static_cast<size_t&g
以下是c#代码:
static void Main(string[] args)
{
uint y = 12;
int x = -2;
if (x > y)
Console.WriteLine("x is greater");
else
Console.WriteLine("y is greater");
}
这是c++代码:
int _tmain(int argc, _TCHAR* argv[])
{
unsigned i