Primary Data Types in C Language



Fundamental Data Types in C

Primary data types, also known as fundamental data types, are built-in data types in C. C compilers support four fundamental data types. They are as follows −

  • Integer
  • Character
  • Floating-point
  • Double precision floating-point

Primary data types are the building blocks for storing and working with different kinds of data in C. Below, we will provide a brief overview of these data types.

Integral Data Types

Integral data types are used to store whole numbers and characters. These are the most important data types for programming because they define how data is represented in memory. They are further classified into two types:

  • Integer data type
  • Character data type

Integer Data Type

Integer data type is specifically used to store whole numbers. The integer storage types include short int, int, and long int, and they can be represented in both positive and negative values depending upon signed and unsigned forms.

Below, the table shows how much memory each data type takes and what the minimum and maximum values we can store are. The control strings for each type are also included.

Type Size (in bytes) Range Control String
short int (or signed short int) 2 -128 to 127 %h
unsigned short int 2 0 to 255 %hu
int (or signed int) 4 -32,768 to 32,767 %d or %i
unsigned int 4 0 to 65,535 %u
long int (or signed long int) 4 -2,147,483,648 to 2,147,483,647 %ld
unsigned long int 4 0 to 4,294,967,295 %lu

Following is an example for integer data types in C.

#include <stdio.h>
int main() {
   // Integer data types
   int num = 100;   // Regular integer
   unsigned int uNum = 200;     // Unsigned integer
   short int sNum = -50;// Short integer
   long int lNum = 1000000;     // Long integer
   // Printing integer values
   printf("Integer Value: %d
", num); // Output: 100 printf("Unsigned Integer Value: %u
", uNum); // Output: 200 printf("Short Integer Value: %d
", sNum); // Output: -50 printf("Long Integer Value: %ld
", lNum); // Output: 1000000 return 0; }

Character Data Type

The character data type is used to store characters only. These characters are internally stored as integers, where each character has an equivalent ASCII value. For example, the character 'A' has an ASCII value of 65.

Working with character data types is important for handling text in your programs. They allow us to manipulate individual characters and work with strings. Strings are just sequences of characters. In C, a string is represented as an array of char types, and it ends with a special null character ('\0') to indicate where the string stops.

Below, the table provides more information on type and its size, storing size limit, control string, etc.

Type Size (in bytes) Range Control String
char (or signed char) 1 -128 to 127 %C
unsigned char 1 0 to 255 %c

Below is the code example of Character data type in C.

#include <stdio.h>

int main() {
   // Store a character
   char ch = 'A'; // We store the letter 'A' in ch
   unsigned char uCh = 'B';   // We store the letter 'B' in uCh

   // Print the character values
   printf("Character: %c
", ch); // Output: A printf("Unsigned Character: %c
", uCh); // Output: B // Print the ASCII values of the characters printf("ASCII of %c: %d
", ch, ch); // Output: 65 printf("ASCII of %c: %d
", uCh, uCh); // Output: 66 return 0; }

Floating-Point Data Type

We use the float data type to store numbers that have fractions. It usually takes 4 bytes (32 bits) in memory. This lets us show real numbers with about 6 to 7 digits of accuracy. We find float useful in cases where we need to work with decimal numbers, like in graphics for things like colors and coordinates.

But, we need to be careful because using float can cause rounding errors. If we do a lot of calculations, these small mistakes can add up and change our final answer. So, even if float saves memory, we should keep its limits in mind. Following table provides the list of data types that stores floating point values −

Type Size (in bytes) Range Control String
float 4 3.4E - 38 to 3.4E + 38 %f
double 8 1.7E - 308 to 1.7E + 308 %lf
long double 16 3.4E - 4932 to 1.1E + 4932 %Lf

Below is the code example of float data type in C.

#include <stdio.h>
int main() {
   float pi = 3.14f; // The 'f' tells us this is a float number
   printf("Value of pi: %.2f
", pi); // This shows: Value of pi: 3.14 return 0; }

Each type has its use based on how precise we need our numbers to be. Using the right type helps our program run better and give correct answers. For many tasks, float is good enough. But for things like science calculations, we often choose double because it gives us more accuracy.

Double Precision Floating-Point Data Type

We use the double data type when we need to be more accurate than with float. It takes 8 bytes (64 bits) in memory. We can store numbers with around 15 to 16 digits of accuracy using double. This is helpful in fields like science or finance, where being precise matters the most.

A great thing about double is that it helps reduce rounding errors. When we do many calculations, small mistakes can add up and make a big difference. Using double means our answers are more accurate when we are working with a lot of data.

For example, if we want to store a exact value like ? (pi), we can use double to keep it as 3.141592653589793.

Below is the code for double precision in C.

#include <stdio.h>
int main() {
   double pi = 3.141592653589793; // We store a precise value in a double
   printf("Value of pi: %.15f
", pi); // We print it with 15 decimal places return 0; }

In short, we choose double when we need more precision. It take more memory than float, but that extra accuracy helps in many situations.

Updated on: 2024-11-11T13:14:18+05:30

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements