DEV Community

Cover image for DATATYPES IN C
Frank
Frank

Posted on

DATATYPES IN C

In C, data types form the structural backbone of any program. Understanding them is critical because they dictate how the compiler allocates memory, interprets data, and determines what operations are valid. In a way, selecting the right data type in C can be compared to choosing the right building material for a structure—it influences efficiency, reliability, and robustness.
Data types define the types of data that a variable can hold. They specify the amount of memory space required and the operations that can be performed on the data. C provides a rich set of built-in data types to handle different types of data.


  1. Types Of Datatypes In C

  2. int

  3. char

  4. float


Types Of Datatypes In C

Data types specify the type of data that can be stored in a variable. They help in defining the variable's memory requirements and the operations that can be performed on the data.

Primary Datatypes

  • int

  • char

  • float

  • double

  • void

  • bool

Derived Datatypes

  • Array

  • Structure

  • Union

  • Pointer

User-defined Datatypes

  • typedef

  • Enum


int-

Image of an int datatype

The int (integer) data type is used to represent whole numbers. It is one of the fundamental and most commonly used data types in C.

This can also be categorized as

NB: This are size modifiers

NB: This are sign modifiers

“int” stands for INTEGER, in this datatype you can store integer values. Examples 1, 123, 10, etc.

Here are the key characteristics and considerations regarding the int data type:

Size and Range

The size of an int variable is platform-dependent, meaning it can vary based on the system architecture. Commonly, an int is 4 bytes on most modern systems.
The range of values that an int can represent is determined by the number of bits allocated to it. It is from -32768 to 32767 For a 32-bit int, the range is typically from -2,147,483,648 to 2,147,483,647. This range is for signed int, by default int means signed integer, because it permits +ve and -ve signs. But for unsigned int you have to specify it in your program.

Unsigned int permits only +ve sign . So the range is from 0 to 65535.

The memory taken by an int datatype is

  • 2 bytes on a 16 bit machine.
  • 4 bytes on a 32 bit machine.
  • 4 bytes on a 64 bit machine. If you want to find out what machine you are working on you can write this code snippet printf(“%lu”, sizeof(int))

NB: %lu is a format specifier for long integer

Short int

Suppose you want to store a number like 1, 2, 3, 8, 7, 63 etc. the space allocated will be smaller than that of long int. The space allocated in memory for a short int value is 1 byte.

Long int

In this case the space allocated will be more than that of short int, because with values like 53782, 1234567, 88107, etc. we will need enough space to store the values in memory. The space allocated in memory for long int is 2 bytes.

How To Know How Many Bits Your Machine Can Store

Suppose your machine is a 2gb RAM machine. To know how many bits your machine can store you can do this:

2 * 1024MB
2 * 1024 * 1024KB
2 * 1024 * 1024 * 1024bytes
2 * 1024 * 1024 * 1024 * 8bits

 On a 2gb RAM machine you can only store 17,179,869,184 bits
Enter fullscreen mode Exit fullscreen mode

Declaration

To declare an int variable, you use the following syntax:

int variable_name;
Enter fullscreen mode Exit fullscreen mode

Initialization

You can initialize an int variable at the time of declaration by using the assignment operator ( = ):

int count = 10;
Enter fullscreen mode Exit fullscreen mode

Operations

int variables support standard arithmetic operations like addition, subtraction, multiplication, and division.

int x = 5;
int y = 3;
int sum = x + y;  // sum is now 8
Enter fullscreen mode Exit fullscreen mode

Formatting for Output

When printing or displaying the value of an int, you can use the %d format specifier in functions like printf:

printf("The value of x is %d\n", x);
Enter fullscreen mode Exit fullscreen mode

Limits

The header file provides constants that represent the limits of various data types. For int, you can use INT_MIN and INT_MAX to get the minimum and maximum values an int can hold.

#include <limits.h>

printf("Minimum value of int: %d\n", INT_MIN);
printf("Maximum value of int: %d\n", INT_MAX);
Enter fullscreen mode Exit fullscreen mode

Modulus Operator

The modulus operator % can be used with int to find the remainder of a division operation:

int remainder = 10 % 3;  // remainder is 1
Enter fullscreen mode Exit fullscreen mode

char-

Image of a char datatype
The char data type is used to represent individual characters. It is one of the fundamental data types and is commonly employed for storing and manipulating characters, such as letters, digits, and special symbols.

Here are the key characteristics and considerations regarding the char data type:

Size

The size of a char variable is always 1 byte / 8bits be it signed char or unsigned char .

By default char is still the same as signed char. This range of signed char is from -128 to 127. The range of unsigned char is from 0 to 255.
The char data type is designed to store a single character from the ASCII character set.

Declaration

To declare a char variable, you use the following syntax:

char character_name;
Enter fullscreen mode Exit fullscreen mode

For Example

char grade;
Enter fullscreen mode Exit fullscreen mode

Initialization

You can initialize a char variable at the time of declaration:

char first_initial = 'A';
Enter fullscreen mode Exit fullscreen mode

Note that character literals are enclosed in single quotes.

ASCII Representation

Each char variable holds a numeric ASCII (American Standard Code for Information Interchange) value that corresponds to a specific character.

For example, the ASCII value for ‘A’ is 65.

Operations

char variables can be used in various operations, including assignment, comparison, and arithmetic operations.

char first_char = 'A';

char second_char = first_char + 1;  // second_char now holds the ASCII value of 'B'
Enter fullscreen mode Exit fullscreen mode

Formatting for Output

When printing or displaying the value of a char, you can use the %c format specifier in functions like printf:

printf("The first initial is %c\n", first_initial);
Enter fullscreen mode Exit fullscreen mode

Character Arrays (Strings)

A sequence of char variables can be used to represent strings (arrays of characters).

char greeting[] = "Hello";
Enter fullscreen mode Exit fullscreen mode

Strings are terminated with a null character (‘\0’), and functions like printf and scanf interpret them accordingly.

Escape Sequences

Special characters, called escape sequences, can be represented using the backslash (). For example, \n represents a newline character.

char newline = '\n';
Enter fullscreen mode Exit fullscreen mode

float-

Image of a float datatype

float data type is used to represent floating-point numbers. Floating-point numbers are numbers that have a decimal point or are expressed in exponential (scientific) notation.

Here are the key characteristics and considerations regarding the float data type:

Size

The size of a float variable is typically 4 bytes on most systems. It may vary depending on the compiler and platform.

The range of a floating number is -3.4e38 to 3.4e38.

Declaration

To declare a float variable, you use the following syntax:

float floating_point_variable;
Enter fullscreen mode Exit fullscreen mode

For Example

float pi;
Enter fullscreen mode Exit fullscreen mode

Initialization

You can initialize a float variable at the time of declaration:

float pi = 3.14159;
Enter fullscreen mode Exit fullscreen mode

Operations

float variables support standard arithmetic operations, including addition, subtraction, multiplication, and division.

float x = 5.0;
float y = 2.5;
float result = x / y;  // result is now 2.0
Enter fullscreen mode Exit fullscreen mode

Formatting for Output

When printing or displaying the value of a float, you can use the %f format specifier in functions like printf:

printf("The value of pi is %f\n", pi);
Enter fullscreen mode Exit fullscreen mode

Precision

float provides single-precision floating-point numbers. Datatype float takes six digits of precisions. It is suitable for a wide range of applications but may have limited precision, especially for very large or very small numbers.
If higher precision is needed, the double data type can be used, which typically provides double-precision floating-point numbers.

float is categorized into

  • double → 8 bytes
  • long double → 10 bytes

Scientific Notation

float numbers can be expressed in scientific notation, using the e or E character to represent the exponent.

float scientific_number = 1.23e-4;  // Equivalent to 0.000123
Enter fullscreen mode Exit fullscreen mode

Limits

The header file provides constants that represent the limits of various floating-point data types. For float, you can use FLT_MIN and FLT_MAX to get the minimum and maximum representable values.

#include <float.h>

printf("Minimum value of float: %e\n", FLT_MIN);
printf("Maximum value of float: %e\n", FLT_MAX);
Enter fullscreen mode Exit fullscreen mode

When you code in C, every choice of a data type affects the performance, portability, and correctness of your program. As a software engineer, one of the most powerful tools in your arsenal is a deep understanding of these types and how they interact with the hardware underneath.

If you enjoyed this journey and would like to show your support, here’s how you can:

Top comments (0)