Data Types in C Programming With Examples

What are Data Types in C?

In C, data types refer to a comprehensive system for declaring variables and functions of various sorts.

The type of a variable dictates how much storage space it takes up and how the stored bit pattern is interpreted.

The following are some examples of typical data types in C:

  • char: The most fundamental data type in C is char. In practically all compilers, it stores a single character and needs a single byte of memory.

  • int: An int variable is used to store an integer, as the name suggests.

  • float: It’s used to store single-precision decimal integers (numbers with floating point values).

  • Double precision: is used to store decimal numbers (numbers with a floating point value).

The types in C can be divided into the following categories:

Name TypeDescription
Basic TypesThey are arithmetic types that are divided into two categories: integer types and floating-point types.
Enumerated typesThey’re arithmetic types again, and they’re utilized throughout the program to define variables that can only assign discrete integer values.
The type voidThe type specifier void denotes the absence of a value.
Derived typesPointer typesArray types, Structure types, Union types, and Function types are some of them.

Different data types can hold numbers within varying numerical ranges.

These ranges may differ between compilers.

Listed below are the ranges, RAM requirements, and format specifiers for the 32-bit gcc compiler.

Basic Types

The table below contains information about typical integer types, including their storage sizes and value ranges.

Data Type 
 
Memory (bytes) 
 
Range 
 
Format Specifier 
 
short int 
 

 
-32,768 to 32,767 
 
%hd 
 
unsigned short int 
 

 
0 to 65,535 
 
%hu 
 
unsigned int 
 

 
0 to 4,294,967,295 
 
%u 
 
int 
 

 
-2,147,483,648 to 2,147,483,647 
 
%d 
 
long int 
 

 
-2,147,483,648 to 2,147,483,647 
 
%ld 
 
unsigned long int 
 

 
0 to 4,294,967,295 
 
%lu 
 
long long int 
 

 
-(2^63) to (2^63)-1 
 
%lld 
 
unsigned long long int 
 

 
0 to 18,446,744,073,709,551,615 
 
%llu 
 
signed char 
 

 
-128 to 127 
 
%c 
 
unsigned char 
 

 
0 to 255 
 
%c 
 
float 
 

 
 %f 
 
double 
 

 
 %lf 
 
long double 
 
16 
 
 %Lf 

Int

Integers are whole numbers that can be either zero, positive, or negative, but never decimal. For instance, 0, -1, 2.

We can declare integer variables using int.

int fname_id;

Here, fname_id is an integer-type variable.

In C programming, multiple variables can be declared simultaneously. For instance,

int fname_id, age;

Typically, the size of an int is 4 bytes (32 bits). In addition, there are 232 distinct states between -2147483648 and 2147483647.

Float and Double

The float and double data types are used to store real numbers.

float num1;
double num2;

Floating-point numbers can be represented exponentially in C. For instance,

float exponential_factor = 22.442e2;

What’s the difference between float and double?

The float data type (single precision float data type) has a size of 4 bytes. Double (double precision float data type) is 8 bytes in size. Float has 2x the precision of double.

float is a 32-bit IEEE 754 single-precision Floating Point Number with 7 decimal digits of precision (one bit for the sign, eight bits for the exponent, and 23* for the value).

double is a 64-bit IEEE 754 double precision Floating Point Number with 15 decimal digits of precision (1 bit for the sign, 11 bits for the exponent, and 52* bits for the value).

Char

Character type variables are declared with the keyword char. As an example,

char name= 'j';

The character variable is 1 byte in size.

Void

A void type is a type that isn’t complete. It can be translated as “nothing” or “no type.” You can think of a void as the absence of something.

If a function does not return anything, for example, the return type should be void.

It’s worth noting that void variables can’t be created.

TypesDescription
Function returns as voidIn C, there are a number of functions that return void, which means they don’t return any value.

The return type of a function with no return value is void. void exit (int status), for example.
Function arguments as voidIn C, there are a number of functions that don’t take any parameters.

A void can be passed to a function that has no parameters.

Int rand(void), for example.
Pointers to voidThe address of an object is represented by a pointer of type void *, but not its type.

For example, the void *malloc(size t size); memory allocation function returns a pointer to void that can be cast to any data type.

Short and Long

You can use the type specifier long if you need to utilize a huge number. Here’s how to do it:

long i;
long long j;
long double k;

Integer values can be stored in variables i and j. Furthermore, k has the ability to hold a floating-point number.

You can use short if you are certain that just a small integer (in the [32,767, +32,767] range) will be used.

short l;

The sizeof() operator can always be used to determine the size of a variable.

Example:

#include <stdio.h>      
int main() {
  short i;
  long j;
  long long k;
  long double l;
  printf("size of short = %d bytes\n", sizeof(i));
  printf("size of long = %d bytes\n", sizeof(j));
  printf("size of long long = %d bytes\n", sizeof(k));
  printf("size of long double= %d bytes\n", sizeof(l));
  return 0;
}

Output:

size of short = 2 bytes
size of long = 8 bytes
size of long long = 8 bytes
size of long double= 16 bytes

You can test the above example here! ➡ C Online Compiler

Signed and Unsigned

Signed and Unsigned are type modifiers in C. You can change a data type’s data storage by utilizing them:

signed – enables both positive and negative numbers to be stored.

unsigned – permits only positive values to be stored.

Example:

// valid codes
unsigned int a = 69;
int b = -79;  // signed int
int c = 89;  // signed int
// invalid code: unsigned int cannot hold negative integers
unsigned int num = -69;

Because we applied the unsigned modifier, the variables x and num can only carry zero and positive numbers.

The sizeof operator can be used to determine the precise size of a type or variable on a specific platform.

The sizeof(type) expression returns the object or type’s storage size in bytes.

An example of getting the size of multiple types on a machine using different constants declared in limits is given below. h file for headers:

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <float.h>
int main(int argc, char** argv) {
    printf("CHAR_BIT    :   %d\n", CHAR_BIT);
    printf("CHAR_MAX    :   %d\n", CHAR_MAX);
    printf("CHAR_MIN    :   %d\n", CHAR_MIN);
    printf("INT_MAX     :   %d\n", INT_MAX);
    printf("INT_MIN     :   %d\n", INT_MIN);
    printf("LONG_MAX    :   %ld\n", (long) LONG_MAX);
    printf("LONG_MIN    :   %ld\n", (long) LONG_MIN);
    printf("SCHAR_MAX   :   %d\n", SCHAR_MAX);
    printf("SCHAR_MIN   :   %d\n", SCHAR_MIN);
    printf("SHRT_MAX    :   %d\n", SHRT_MAX);
    printf("SHRT_MIN    :   %d\n", SHRT_MIN);
    printf("UCHAR_MAX   :   %d\n", UCHAR_MAX);
    printf("UINT_MAX    :   %u\n", (unsigned int) UINT_MAX);
    printf("ULONG_MAX   :   %lu\n", (unsigned long) ULONG_MAX);
    printf("USHRT_MAX   :   %d\n", (unsigned short) USHRT_MAX);
    return 0;
}

On Compiler, compiling and running the above program yields the following result:

CHAR_BIT : 8
CHAR_MAX : 127
CHAR_MIN : -128
INT_MAX : 2147483647
INT_MIN : -2147483648
LONG_MAX : 9223372036854775807
LONG_MIN : -9223372036854775808
SCHAR_MAX : 127
SCHAR_MIN : -128
SHRT_MAX : 32767
SHRT_MIN : -32768
UCHAR_MAX : 255
UINT_MAX : 4294967295
ULONG_MAX : 18446744073709551615
USHRT_MAX : 65535

You can test the above example here! ➡ C Online Compiler

Floating-Point Types

The table below lists typical floating-point types, along with their storage capacities, value ranges, and precision.

TypeStorage SizeValue rangePrecision
float4 byte1.2E-38 to 3.4E+386 decimal places
double8 byte2.3E-308 to 1.7E+30815 decimal places
long double10 byte3.4E-4932 to 1.1E+493219 decimal places

In your applications, the header file floats.h specifies macros that allow you to use these values and other data about the binary representation of real numbers.

The following example prints the amount of storage space a float type consumes as well as its range values.

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <float.h>
int main(int argc, char** argv) {
    printf("Storage size for float : %d \n", sizeof(float));
    printf("FLT_MAX     :   %g\n", (float) FLT_MAX);
    printf("FLT_MIN     :   %g\n", (float) FLT_MIN);
    printf("-FLT_MAX    :   %g\n", (float) -FLT_MAX);
    printf("-FLT_MIN    :   %g\n", (float) -FLT_MIN);
    printf("DBL_MAX     :   %g\n", (double) DBL_MAX);
    printf("DBL_MIN     :   %g\n", (double) DBL_MIN);
    printf("-DBL_MAX     :  %g\n", (double) -DBL_MAX);
    printf("Precision value: %d\n", FLT_DIG );
    return 0;
}

On Compiler, compiling and running the above program yields the following result:

Storage size for float : 4
FLT_MAX : 3.40282e+38
FLT_MIN : 1.17549e-38
-FLT_MAX : -3.40282e+38
-FLT_MIN : -1.17549e-38
DBL_MAX : 1.79769e+308
DBL_MIN : 2.22507e-308
-DBL_MAX : -1.79769e+308
Precision value: 6

You can test the above example here! ➡ C Online Compiler

Summary

With the help of examples, we learned about various data types in C in this article. Additionally, we talked about each data type’s examples.

We anticipate that this knowledge of C data types will be useful to you as you write effective C programs.

In the next following post, I’ll create a variable in C and try to explain its many components in depth.

I hope you enjoy this post on Data Types in C, in which I attempt to illustrate the language’s core grammar.


Leave a Comment