DEV Community

Cover image for Basic Data Types in C
Mukesh Kumar
Mukesh Kumar

Posted on

Basic Data Types in C

πŸŽ™οΈ Introduction

Hello everyone!

Welcome to this important chapter β€” Basic Data Types in C.

In the previous lesson, we understood what a data type is and why it is necessary in programming. Now, in this chapter, we will focus specifically on the basic or primary data types in C language.

These data types are the foundation of every C program. Almost every program you write will use at least one of these basic...Read More

πŸ”Ή Step 1: What Are Basic Data Types?

Basic data types, also known as primary data types, are the fundamental data types provided by the C language.

They are used to store simple values such as:

Whole numbers

Decimal numbers

Characters

In C, the main basic data types are...Read More

πŸ”Ή Step 2: The int Data Type

The first and most commonly used data type is int.

The int data type is used to store whole numbers, also called integers. These are numbers without decimal points.

Examples of integers:

10

-5

1000

0

The int data type is commonly used for...Read More

πŸ”Ή Step 3: The float Data Type

The next basic data type is float.

The float data type is used to store decimal numbers, also known as floating-point numbers.

Examples:

3.14

9.5

-2.75

The float data type is used when precision up to a certain...Read More

πŸ”Ή Step 4: The double Data Type

The double data type is similar to float but provides higher precision.

It is used to store larger decimal numbers or numbers that require more accuracy.

Examples:

3.1415926535

12345.6789123

The double data type typically occupies 8 bytes of...Read More

πŸ”Ή Step 5: The char Data Type

The char data type is used to store a single character.

Examples:

'A'

'b'

'5'

'@'

The char data type occupies 1 byte of memory.

Even though char stores characters, internally it stores...Read More

πŸ”Ή Step 6: Memory and Size Considerations

Each basic data type occupies a different amount of memory.

Typically:

char β†’ 1 byte

int β†’ 4 bytes

float β†’ 4 bytes

double β†’ 8 bytes

Choosing the correct data type is important because:

Using a larger data type than necessary wastes memory...Read More

πŸ”Ή Step 7: Modifiers of Basic Data Types

C also provides modifiers to change the size or range of basic data types.

For example:

short int

long int

unsigned int

signed int

These modifiers help control the range of values stored...Read More

πŸ”Ή Step 8: Real-Life Example of Using Basic Data Types

Let’s consider a simple student record:

Age β†’ int

Percentage β†’ float

Total marks β†’ int

Grade β†’ char

Each piece of information requires a specific data type.

If we store percentage in int, we lose decimal accuracy...Read More

πŸ”Ή Step 9: Importance of Basic Data Types

Basic data types are important because:

They are used in almost every program.

They form the base of arrays and structures.

They are essential for calculations.

They determine memory usage.

They affect program performance...Read More

πŸ”Ή Step 10: Common Mistakes with Basic Data Types

Some common mistakes include:

Storing decimal values in int.

Using float when double is required.

Ignoring overflow limits.

Misunderstanding character storage....Read More

πŸ“Œ Summary

In this chapter, we learned:

Basic data types are the fundamental data types in C.

The main basic data types are int, float, double, and char.

Each data type has a specific purpose.

Each occupies a specific amount of memory...Read More

Top comments (0)