您的位置:首页 > 编程语言

磕代码:输出不同整型数据类型在内存中占多大(字节)

2020-08-05 15:10 204 查看


c:

#include<stdio.h>
int main()
{
printf("The size of short is %d bytes.\n",sizeof(short));
printf("The size of int is %d bytes.\n",sizeof(int));
printf("The size of long is %d bytes.\n",sizeof(long));
printf("The size of long long is %d bytes.",sizeof(long long));
return 0;
}

注:字符串用双引号,字符用单引号。
C语言pirntf里用单引号会报错:数组越界。
sizeof的使用。
c++:

#include <bits/stdc++.h>
using namespace std;

int main() {
//使用sizeof运算符输出各种类型的大小
printf("The size of short is %d bytes.\n", sizeof(short));
printf("The size of int is %d bytes.\n", sizeof(int));
printf("The size of long is %d bytes.\n", sizeof(long));
printf("The size of long long is %d bytes.\n", sizeof(long long));
}

注:也可以用printf。
Java:

public class Main{
public static void main(String[]args){

System.out.println("The size of short is "+Short.SIZE/8+" bytes.");
System.out.println("The size of int is "+Integer.SIZE/8+" bytes.");
System.out.println("The size of long is "+Long.SIZE/8+" bytes.");
System.out.println("The size of long long is "+Long.SIZE/8+" bytes.");
}
}

注:SIZE大写;
求的是位数,算字节数要再除以8;
println自带换行;
long和longlong是一样的?;

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐