Data Types in C

Here we will discuss data types in the C language. How many types of data C language supports and which new data types were introduced in C99?

A keyword that is used for creating variables for storing single and multiple values is called data types.

Creating a variable for storing single and multiple values in a program as part of performing one operation. In the program, to perform any operation first we must store values. To store values we must allocate memory. Here we need to provide the information above the number of bytes and type of memory that must be allocated to store a value.

To specify the number of bytes and memory types to the compiler and operating system. we must use some set of keywords. These sets of keywords are collectively called data types. Hence we can say data type is used for allocating memory for storing the value in a program by specifying the required numbers of bytes and memory type.

In C language, data types can be classified into three parts:-

  1. Primitive/Basic data types
  2. Derived data types
  3. User-defined data types
data types in C 1

Also See:- Identifiers and Data Types Quiz in C

Primitive Data Types

In C there are three types of basic data types. character data type, Integer data type, floating-point data, and void.

primitive data types in C language
Variable TypeKeywordFormatSize (in Bytes)
Short Integershort%d2
Integerint%d2 or 4
Long Integerlong%ld4
Floating Pointfloat%f4
Double-Precision Floating-Pointdouble%f8
Long Double-Precision Floating-Pointlong double%lf12
Characterchar%c1

Character Data Type

In C language, to store character data types keyword char is used. For character type variables and single-character constants, 1 byte (8 bits) of memory space is allocated. It may be observed that small int value may be stored in char variables and char values may be stored in int variables.

The range of char data types is 0 to 255 for unsigned char and -128 to 127 for signed char. It is classified under the integral data type as the storage occurs in the form of ASCII values which are used to represent every character. Thus ‘a’ has the decimal value 97. There are two variations of char data type: unsigned char data type and signed char data type.

Example of char data types:- ‘m’, ‘A’, ‘5’, ‘@’, ‘?’ e.t.c. Note that all are inside the single quotation.

  • “a” is not a char data type because it is inside double quotation not in the single quotation.
  • ‘abc’ is not a char data type because inside a single quotation only one character must be present.
  • ‘’ is not a char data type, a blank single quotation is not a valid character.
  • ‘ ’ is a char data type, space is inside a single quotation and space is a valid character.
  • m is not a char data type, it is a variable because it is not inside a single quotation.
Data type Format specifier size(in byte) Range
unsigned char %c 1 0 to 255
char %c 1 -127 to 128
signed char %c 1 -127 to 128
#include<stdio.h>
int main ()
{
   char ch = 'A';
   char gender='M';
   char number='5';
   printf("ch = %c\n",ch);
   printf("Gender = %c\n",gender);
   printf("Number = %c\n",number);

   // display these character value into decimal use %d

   printf("Decimal value of Gender = %d\n",gender);
   printf("Decimal value of Number = %d\n",number);

   return 0;
}

Output:-

ch = A
Gender = M
Number = 5
Decimal value of Gender = 77
Decimal value of Number = 53

Integer data type

The keyword int is used to declare integer data type. If integer data is small then we can use keyword short or short int and to store long integer number we can use long or long int keyword and to store very long number we can use long long or long long int keyword.

#include<stdio.h>
int main ()
{
     short a = 10; //short data type
     short int a1 = 20;
     int a2 = 1000; //int data type
     long a3 = 100000000; //long data type
     long int a4 = 200000000;
     long long a5 = 500000000000;//long long data type
     long long int a6 = 900000000000;

     printf("%d\n",a);
     printf("%d\n",a1);
     printf("%d\n",a2);
     printf("%ld\n",a3);
     printf("%ld\n",a4);
     printf("%lld\n",a5);
     printf("%lld\n",a6);

     return 0;
}

Output:-

10
20
1000
100000000
200000000
500000000000
900000000000

Data type Format specifier Size (in bytes)
short int %d 2
signed short int %d 2
unsigned short int %u 2
int %d 4
signed int %d 4
unsigned int %u 4
long int %ld 8
signed long int %ld 8
unsigned long int %lu 8
long long int %lld 8
unsigned long long int %llu 8

The range in possible values depends on the computer system. The size of an int is usually the same as the word length of the execution environment of the program. The size of data type int is 2 bytes in a 32-bit environment and 4 bytes in a 64-bit environment.

If a signed integer has n bits, it can contain a number between -2n-1 to +(2n-1 -1)

In the binary number system, an unsigned integer containing “n” bits can have a value between 0 to 2n-1

Floating point data type

By default, every floating-point number is treated as a double data type. Float and long double data types are also used for floating-point.

Sometimes every real-world value can’t be stored using an integer, floating-point values are required. For example to calculate the weight of a person floating-point values are required.

Data type Format Specifier Size (in bytes)
float %f 4
double %f 8
long double %Lf 16

Generally, the size of the float data type is 4 bytes and the double data type is 8 bytes. Floating-point variables have a precision of 6 digits whereas the precision of double is 15 digits. Precision describes the number of significant decimal places that a floating value carries.

#include<stdio.h>
int main()
{
   float n1 = 9.5;
   double n2 = 9000.56;
   long double n3 = 44141210.6562;

   printf("%f\n",n1);
   printf("%f\n",n2);
   printf("%Lf",n3);

   return 0;
}

Output:-

9.500000
9000.560000
44141210.656200

void

As the name indicates this type has no values. Most of the time it is used to indicate that a function does not return any value. All other primitive data types short, int, long int, float, double and long double can be used for both calculation (like storing values to a variable) and returning from a function but void can only be used for returning from a function. Void can’t be used for storing and calculation in a program.

Derived and User-defined data types

The derived data types are pointers, functions, and arrays.

Using an array we can store more than one value of the same data type in one variable. The function is a small program is used to do a particular task. In C a big program is divided into several small subroutines/functions/procedures. Hence C is a function-oriented programming language.

Advantage of Function:-

  • Modularity:- Dividing a big program into small modules.
  • Reusability:- Write once, use many times.
  • Simplicity:- Easy to read instructions.
  • Efficiency:- Program performance increased.

User-defined data types are generally to be defined by the user itself. For example structures, unions, and enumerated types.

New data types introduced in C99

Bool

With the release of C99, the C language incorporated a Boolean type. Boolean types represent only two values: true or false. Prior to C99, C used integers to represent the Boolean values: a nonzero number (both positive or negative) was used to represent true, and zero was used to represent false. The boolean type, which is referred to by the keyword bool, is stored in memory as 0 (false) and 1(true).

#include<stdio.h>
#include<stdbool.h>
int main()
{
   bool x = true;
   bool y = false;
   printf("%d\t", x);
   printf("%d\n", y);
   return 0;
}

Output:-

1 0

Don’t forget to add the header file “stdbool.h” otherwise you will get a compile-time error. The keywords bool, true and false are defined inside the header file “stdbool.h

long long int

C99 provides two additional integer types long long int and unsigned long long int. For long long, the C99 standard specified at least 8 bytes (64 bits) to support. C requires that the following relationship always be true:-

sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long)

float complex, double complex, long double complex

C defined the complex type, which is implemented by most compilers. A complex number is a combination of a real and imaginary number. The complex type, like the real type, can be of three different sizes: float complex, double complex, and long long complex.

#include<stdio.h>
// standard Library of complex numbers
#include<complex.h>
int main()
{
   double complex x = 5 + 7 * I;
   printf("Real part = %.2f\n",creal(x));
   printf("Imaginary part = %.2f\n",cimag(x));
   return 0;
}

Here we are working with double so,
creal(z1): get the real part
cimag(z1): get the imaginary part

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *