DATATYPES IN C (int, char, float)

Dev Frank
6 min readFeb 5, 2024

--

In C programming, 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.

Datatype is the storage or memory to be allocated to a variable in C.

Datatypes are reserved space in memory according to our requirement. It tells you the type of data you you want to store in memory.

Types Of Datatypes In C

  1. Primary Datatypes
  • int
  • char
  • float
  • double
  • void
  • bool

2. Derived Datatypes

  • Arrays
  • Structure
  • Union
  • Pointers

3. User-defined Datatypes

  • typedef
  • Enum

int-

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

  • short int
  • long int

NB: This are size modifiers

  • signed int
  • unsigned int

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:

1. 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

2. Declaration:

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

int variable_name;

3. Initialization:

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

int count = 10;

4. 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

5. 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);

6. Limits:

The <limits.h> 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);

7. 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

char —

In C programming, 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:

1. 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.

2. Declaration:

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

char character_name;

For Example

char grade;

3. Initialization:

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

char first_initial = 'A';

Note that character literals are enclosed in single quotes.

4. 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.

5. 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'

6. 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);

7. Character Arrays (Strings):

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

char greeting[] = "Hello";

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

8. Escape Sequences:

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

char newline = '\n';

float —

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:

1. 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.

2. Declaration:

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

float floating_point_variable;

For Example

float temperature;

3. Initialization:

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

float pi = 3.14159;

4. 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

5. 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);

6. 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

7. 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

8. Limits:

The <float.h> 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);

--

--

Dev Frank

Passionate tech enthusiast, diving deep into the world of software engineering. Thrilled to share insights with the world. A Software engineering student.