Is the size of C “int” 2 bytes or 4 bytes?

后端 未结 13 2516
不知归路
不知归路 2020-11-22 10:52

Does an Integer variable in C occupy 2 bytes or 4 bytes? What are the factors that it depends on?

Most of the textbooks say integer variables occupy 2 bytes. But whe

13条回答
  •  失恋的感觉
    2020-11-22 11:03

    Mostly it depends on the platform you are using .It depends from compiler to compiler.Nowadays in most of compilers int is of 4 bytes. If you want to check what your compiler is using you can use sizeof(int).

    main()
    {
        printf("%d",sizeof(int));
        printf("%d",sizeof(short));
        printf("%d",sizeof(long));
    }
    

    The only thing c compiler promise is that size of short must be equal or less than int and size of long must be equal or more than int.So if size of int is 4 ,then size of short may be 2 or 4 but not larger than that.Same is true for long and int. It also says that size of short and long can not be same.

提交回复
热议问题