歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> C語言查看各種數據類型的size

C語言查看各種數據類型的size

日期:2017/3/1 9:20:12   编辑:Linux編程

C語言查看各種數據類型的size

intptr_t 定義在/usr/include/stdint.h,

#if __WORDSIZE == 64

# ifndef __intptr_t_defined
typedef long int intptr_t;
# define __intptr_t_defined
# endif
typedef unsigned long int uintptr_t;
#else
# ifndef __intptr_t_defined
typedef int intptr_t;
# define __intptr_t_defined
# endif
typedef unsigned int uintptr_t;
#endif

#include <stdio.h>

#include <stdint.h>

int main(void)
{
int *ptr;
printf("sizeof int is %d\n",sizeof(int));
printf("sizeof char is %d\n",sizeof(char));
printf("sizeof short is %d\n",sizeof(short));
printf("sizeof long is %d\n",sizeof(long));
printf("sizeof float is %d\n",sizeof(float));
printf("sizeof double is %d\n",sizeof(double));
printf("sizeof pointer is %d\n",sizeof(ptr));
printf("sizeof unsiged int is %d\n",sizeof(unsigned int));
//store pointers in integer type,use size_t,INT_PTR,intptr_t;
printf("sizeof size_t is %d\n",sizeof(size_t));
//intptr_t defines in stdint.h
printf("sizeof intptr_t is %d\n",sizeof(intptr_t));
//INT_PTR only exist in windows
//printf("sizeof INT_PTR is %d\n",sizeof(INT_PTR));

}

查看各種數據類型的MAX 和MIN 值

#include <stdio.h>

#include <limits.h>
#include <float.h>
int main()
{
printf("The value of INT_MAX is %i\n", INT_MAX);
printf("The value of INT_MIN is %i\n", INT_MIN);
printf("An int takes %d bytes\n", sizeof(int));

printf("The value of FLT_MAX is %f\n", FLT_MAX);
printf("The value of FLT_MIN is %.50f\n", FLT_MIN);
printf("A float takes %d bytes\n", sizeof(float));

printf("The value of CHAR_MAX is %d\n", CHAR_MAX);
printf("The value of CHAR_MIN is %d\n", CHAR_MIN);
printf("A CHAR takes %d bytes\n", sizeof(char));

printf("The value of DBL_MAX is %f\n", DBL_MAX);
printf("The value of DBL_MIN is %.50f\n", DBL_MIN);
printf("A double takes %d bytes\n", sizeof(double));

printf("The value of SHRT_MAX is %d\n", SHRT_MAX);
printf("The value of SHRT_MIN is %d\n", SHRT_MIN);
printf("A short takes %d bytes\n", sizeof(short));

printf("The value of LONG_MAX is %Ld\n", LONG_MAX);
printf("The value of LONG_MIN is %Ld\n", LONG_MIN);
printf("A long takes %d bytes\n", sizeof(long));
return 0;
}

在stdint.h 裡面定義了很多int*_t 和uint*_t 類型。

#include <apue.h>

#include <stdint.h>
#include <limits.h>

int main(int argc, char *argv[])
{
printf("CHAR_BIT is %d\n\n",CHAR_BIT);
printf("sizeof(char) is %d\n",sizeof(char));
printf("sizeof(short) is %d\n",sizeof(short));
printf("sizeof(int) is %d\n",sizeof(int));
printf("sizeof(long) is %d\n",sizeof(long));
printf("sizeof(long long) is %d\n\n",sizeof(long long));

printf("sizeof(int8_t) is %d\n",sizeof(int8_t));
printf("sizeof(int16_t) is %d\n",sizeof(int16_t));
printf("sizeof(int32_t) is %d\n",sizeof(int32_t));
printf("sizeof(int64_t) is %d\n\n",sizeof(int64_t));

printf("sizeof(uint8_t) is %d\n",sizeof(uint8_t));
printf("sizeof(uint16_t) is %d\n",sizeof(uint16_t));
printf("sizeof(uint32_t) is %d\n",sizeof(uint32_t));
printf("sizeof(uint64_t) is %d\n",sizeof(uint64_t));

char a,b;
printf("sizeof(a+b) is %d\n",sizeof(a+b));
}

Copyright © Linux教程網 All Rights Reserved