Curriculum
Course: C basic
Login

Curriculum

C basic

C Introduction

0/1

C Get Started

0/1

C Comments

0/1

C Constants

0/1

C Operators

0/1

C Break and Continue

0/1

C User Input

0/1

C Memory Address

0/1

C Structures

0/1
Text lesson

Memory Size

Get the Memory Size

In the data types chapter, we discussed how the memory size of a variable fluctuates based on its type.

Data Type

Size

int

2 or 4 bytes

float

4 bytes

double

8 bytes

char

1 byte

Memory size denotes the space occupied by a data type in the computer’s memory.

To obtain the size, measured in bytes, of a data type or variable, utilize the sizeof operator.

Example

int myInt;
float myFloat;
double myDouble;
char myChar;

printf(“%lu\n”, sizeof(myInt));
printf(“%lu\n”, sizeof(myFloat));
printf(“%lu\n”, sizeof(myDouble));
printf(“%lu\n”, sizeof(myChar)); 

 

It’s important to note that we employ the %lu format specifier to print the result of the sizeof operator instead of %d. This is because the compiler anticipates the sizeof operator to return a long unsigned int (%lu) rather than an int (%d). While %d might function on certain computers, it’s more reliable to use %lu.

Why Should I Know the Size of Data Types?

Utilizing the appropriate data type for the intended purpose conserves memory and enhances program performance.

Later in this tutorial, you’ll delve deeper into the sizeof operator and its various applications.