sizeof

Checking the sizeof an integer type in the preprocessor

自闭症网瘾萝莉.ら 提交于 2020-01-02 01:01:09
问题 How can I check the size of an unsigned in the preprocessor under g++? sizeof is out of the question since it is not defined when during preprocessing. 回答1: This may not be the most elegant method, but one thing that you may be able to leverage is UINT_MAX defined in "limits.h". That is, ... if UINT_MAX == 65535, then you would know that sizeof (unsigned) = 2 if UINT_MAX == 4294967295, then you would know that sizeof (unsigned) = 4. and so on. As I said, not elegant, but it should provide

在C语言中,double、long、unsigned、int、char类型数据所占字节数

▼魔方 西西 提交于 2020-01-01 09:07:05
和机器字长及编译器有关系: 所以,int,long int,short int的宽度都可能随编译器而异。但有几条铁定的原则(ANSI/ISO制订的): 1 sizeof(short int)<=sizeof(int) 2 sizeof(int)<=sizeof(long int) 3 short int至少应为16位(2字节) 4 long int至少应为32位。 unsigned 是无符号的意思。 例如: 16位编译器 char :1个字节 char*(即指针变量): 2个字节 short int : 2个字节 int: 2个字节 unsigned int : 2个字节 float: 4个字节 double: 8个字节 long: 4个字节 long long: 8个字节 unsigned long: 4个字节 32位编译器 char :1个字节 char*(即指针变量): 4个字节(32位的寻址空间是2^32, 即32个bit,也就是4个字节。同理64位编译器) short int : 2个字节 int: 4个字节 unsigned int : 4个字节 float: 4个字节 double: 8个字节 long: 4个字节 long long: 8个字节 unsigned long: 4个字节 64位编译器 char :1个字节 char*(即指针变量): 8个字节

在C语言中,double、long、unsigned、int、char类型数据所占字节数

北战南征 提交于 2020-01-01 09:06:31
和机器字长及编译器有关系: 所以,int,long int,short int的宽度都可能随编译器而异。但有几条铁定的原则(ANSI/ISO制订的): 1 sizeof(short int)<=sizeof(int) 2 sizeof(int)<=sizeof(long int) 3 short int至少应为16位(2字节) 4 long int至少应为32位。 unsigned 是无符号的意思。 例如: 16位编译器 char :1个字节 char*(即指针变量): 2个字节 short int : 2个字节 int: 2个字节 unsigned int : 2个字节 float: 4个字节 double: 8个字节 long: 4个字节 long long: 8个字节 unsigned long: 4个字节 32位编译器 char :1个字节 char*(即指针变量): 4个字节(32位的寻址空间是2^32, 即32个bit,也就是4个字节。同理64位编译器) short int : 2个字节 int: 4个字节 unsigned int : 4个字节 float: 4个字节 double: 8个字节 long: 4个字节 long long: 8个字节 unsigned long: 4个字节 64位编译器 char :1个字节 char*(即指针变量): 8个字节

How does sizeof work for different data types when added and calculated? [duplicate]

大兔子大兔子 提交于 2020-01-01 07:05:10
问题 This question already has answers here : What happens here? sizeof(short_int_variable + char_variable) (5 answers) Closed 5 years ago . #include <stdio.h> int main() { short int i = 20; char c = 97; printf("%d, %d, %d\n", sizeof(i), sizeof(c), sizeof(c + i)); return 0; } The output of this code is 2, 1, 4 According to me it should be 2, 1, 2 because char + short int is short int and size of short int is 2 . 回答1: According to C standard integral promotion rules, the type of the expression c +

find sizeof char array C++

≡放荡痞女 提交于 2020-01-01 05:33:06
问题 im trying to get the sizeof char array variable in a different function where it was initialize however cant get the right sizeof. please see code below int foo(uint8 *buffer){ cout <<"sizeof: "<< sizeof(buffer) <<endl; } int main() { uint8 txbuffer[13]={0}; uint8 uibuffer[4] = "abc"; uint8 rxbuffer[4] = "def"; uint8 l[2]="g"; int index = 1; foo(txbuffer); cout <<"sizeof after foo(): " <<sizeof(txbuffer) <<endl; return 0; } the output is: sizeof: 4 sizeof after foo(): 13 desired output is:

ping 实现设计---ICMP

江枫思渺然 提交于 2020-01-01 03:42:00
发送ICMP报文时,必须程序自己计算校验和,将它填入ICMP头部对应的域中。 校验和的计算方法:   将数据以字为单位累加到一个双字中,如果数据长度为奇数,最后一个字节将被扩展到字,累加的结果是一个双字,最后将这个双字的高16位,低16位相加后取反,便得到了校验和。 下面是checksum的计算校验和的代码: USHORT checksum(USHORT* buff, int size) { unsigned long cksum = 0; while(size>1) { cksum += *buff++; size -= sizeof(USHORT); } // 是奇数 if(size) { cksum += *(UCHAR*)buff; } // 将32位的chsum高16位和低16位相加,然后取反 cksum = (cksum >> 16) + (cksum & 0xffff); cksum += (cksum >> 16); return (USHORT)(~cksum); } Ping程序实例: Ping用来检查主机是否存在,是否可达。 下面是Ping的执行步骤: 1 创建协议类型为IPPROTO_ICMP的原始套接字 2 创建并初始化ICMP封包 3 调用sendto函数向远程主机发送ICMP请求 4 调用recvfrom函数接收ICMP响应 完整代码如下: /////

“sizeof” to know the size of an array doesn't work in a function in C [duplicate]

岁酱吖の 提交于 2019-12-31 05:43:06
问题 This question already has answers here : Why isn't the size of an array parameter the same as within main? (13 answers) Closed 4 years ago . int main() { int laiArreglo[] = {5,8,2,3,1,4,6,9,2,10}, liElemento; printf("\nInsert the number: "); scanf("%d", &liElemento); ShowNumber(laiArreglo); return 0; } void ShowNumber(int laiArreglo[]) { int liContador; printf("\nNumbers: "); for (liContador = 0; liContador < sizeof (laiArreglo) / sizeof (int); liContador++) { printf("%d ", laiArreglo

sizeof argv[1] not working

自闭症网瘾萝莉.ら 提交于 2019-12-31 04:17:04
问题 I'm really new to C and all I know is that the error is related to oldname and newname not be initialized #include <stdio.h> int main (int argc, char const *argv[]) { int result; int lengthOne; int lengthTwo; lengthOne = sizeof(argv[0]); lengthTwo= sizeof(argv[1]); char oldname[lengthOne] = argv[0]; char newname[lengthOne] = argv[1]; result = rename(oldname, newname); if (result == 0) { puts "File renamed"; } else { perror "ERROR: Could not rename file"; } return 0; } app.c: In function ‘main

Array length problem

旧巷老猫 提交于 2019-12-31 01:48:29
问题 I was reading a csc placement paper where I read a question related to c language array's sizeof() operator. Answer was something else then i expected it to be. int DIMension(int array[]) { return sizeof(array )/ sizeof(int); } main() { int arr[10]; printf(“Array dimension is %d”, DIMension(arr)); } This program in c language prints 1 as the answer. Why is that happening? 回答1: Because int array[] is just a pointer and it's size is same as of int . I think you expected that size of arr will

11-enum,sizeof,typedef分析

独自空忆成欢 提交于 2019-12-30 22:46:06
注:博客中内容主要来自《狄泰软件学院》,博客仅当私人笔记使用。 测试环境:Ubuntu 10.10 GCC版本:4.4.5 一、枚举类型的使用方法 1) enum 是C语言中的一种自定义类型 2) enum 值是可以根据需要自定义的整型值 3)第一个定义的 enum 值默认为0 4)默认情况下的 enum 值是在前一个定义值的基础上加1 5) enum 类型的变量只能取定义时的离散值 enum Color { GREEN, RED = 2, BLUE //实际为3 }; ​ enum Color c = GREEN; printf("%d\n", c); 除了用自身成员赋值,还可以用其它数值赋值。 二、枚举类型的特殊意义 1) enum 中定义的值是C语言中真正意义上的常量 2)在工程中 enum 多用于定义整型常量 enum //无名枚举,用于定义常量 { ARRAY_SIZE = 10, //定义数组大小 }; ​ int array[ARRAY_SIZE] = {0}; //通过编译,证明是常量 int i = 0; for(i=0; i<ARRAY_SIZE; i++) { array[i] = i + 1; } 实例分析 enum的使用 11-1.c #include <stdio.h> ​ enum //定义一个整型常量10 { ARRAY_SIZE = 10 /