Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Sizeof | Data
C Basics

Sizeof

Data Size

The sizeof() function is a staple in C programming. It helps determine the size (in bytes) of the specified object or type.

For instance, let's see how many bytes the int data type occupies:

c

main.c

The int data type occupies 4 bytes.

Note

Bear in mind that different compilers might allocate varying byte sizes for the same data type.

Bits

A bit is the most basic unit of data a computer uses. Every byte consists of eight bits.

It was collectively decided by engineers to equate one byte to 8 bits because this configuration conveniently represents decimal numbers.

You've likely heard of the binary number system, which forms the foundation of computer operations.

At its essence, the goal is to represent numerical values we use in our daily lives using combinations of zeros and ones. Any number can be represented as a combination of powers of two.

For instance, the number 7 can be portrayed as "111", and here's the breakdown:

The values 0 or 1 don't inherently have mathematical significance; they merely indicate a bit's state.

  • 0 – the bit is inactive;
  • 1 – the bit is active.

The number 113, in binary, appears as "01110001":

Data Types

What distinguishes different data types? – Their byte size!

c

Main.c

You can utilize the sizeof() function on an array to ascertain its size:

c

main.c

An array with 10 integer elements occupies 40 bytes, meaning each individual element consumes 4 bytes. If you divide the total array size by the size of one of its elements, you'll determine the array's element count:

c

main.c

Everything was clear?

Section 2. Chapter 7
course content

Course Content

C Basics

Sizeof

Data Size

The sizeof() function is a staple in C programming. It helps determine the size (in bytes) of the specified object or type.

For instance, let's see how many bytes the int data type occupies:

c

main.c

The int data type occupies 4 bytes.

Note

Bear in mind that different compilers might allocate varying byte sizes for the same data type.

Bits

A bit is the most basic unit of data a computer uses. Every byte consists of eight bits.

It was collectively decided by engineers to equate one byte to 8 bits because this configuration conveniently represents decimal numbers.

You've likely heard of the binary number system, which forms the foundation of computer operations.

At its essence, the goal is to represent numerical values we use in our daily lives using combinations of zeros and ones. Any number can be represented as a combination of powers of two.

For instance, the number 7 can be portrayed as "111", and here's the breakdown:

The values 0 or 1 don't inherently have mathematical significance; they merely indicate a bit's state.

  • 0 – the bit is inactive;
  • 1 – the bit is active.

The number 113, in binary, appears as "01110001":

Data Types

What distinguishes different data types? – Their byte size!

c

Main.c

You can utilize the sizeof() function on an array to ascertain its size:

c

main.c

An array with 10 integer elements occupies 40 bytes, meaning each individual element consumes 4 bytes. If you divide the total array size by the size of one of its elements, you'll determine the array's element count:

c

main.c

Everything was clear?

Section 2. Chapter 7
some-alt