Menu Close

Data Types in C Program

Posted in C Programming

C language is a statically-typed programming language, so the variable type must be declared before use. C provides several basic data types, including integer types, floating-point types, character types, and boolean types. In addition, advanced data types such as structures, unions, and enumerations can be used to organize and process data.

C Data Types can be divided into three types:

  • Primary data types
  • Derived data types
  • User-defined data types

1. Primary Data Types

Basic data types are data types that are already defined in a programming language and are also known as built-in data types. These data types are fundamental building blocks of any programming language, and many derived data types are built from these basic types.

We can use the sizeof() operator and the format specifier “%lu” to check the memory size (in bytes) of any variable.

Basic data types include:

  • Char data type  – character data type. The char type refers to a single character, and its type declaration uses the char keyword.
  • Integer data type – integer types are used to represent larger integers, and their type declaration uses the int keyword.
  • Floating-point data type – any value with a decimal point will be interpreted by the compiler as a floating-point number. The term “floating-point number” refers to a number stored in the form of m * be, where m is the fractional part, b is the base (usually 2), and e is the exponent part. This form is a combination of precision and numerical range, and can represent very large or very small numbers; the floating-point type declaration uses the float keyword and can be used to declare floating-point variables.
  • Void type – represents function type, with no return value.

C language supports signed and unsigned characters. The memory size of basic data types may vary depending on whether the system is 32 or 64-bit.

Related:   Loop Statements in C Program
Data Type Memory (bytes) Range Format Specifier
short int 2 -32,768 to 32,767 %hd
unsigned short int 2 0 to 65,535 %hu
unsigned int 4 0 to 4,294,967,295 %u
int 4 -2,147,483,648 to 2,147,483,647 %d
long int 4 -2,147,483,648 to 2,147,483,647 %ld
unsigned long int 4 0 to 4,294,967,295 %lu
long long int 8 -(2^63) to (2^63)-1 %lld
unsigned long long int 8 0 to 18,446,744,073,709,551,615 %llu
signed char 1 -128 to 127 %c
unsigned char 1 0 to 255 %c
float 4 1.2E-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

 

In addition to the basic data types, C also provides advanced data types such as structures, unions, and enumerations, which can be used to organize and process more complex data structures. Structures and unions can combine multiple data types to form a new data type, while enumerations can be used to define a set of named integer constants.

2. Derived Data Types

Derived data types are defined by constructing a new type using one or more existing data types that have been previously defined. In other words, a constructed type’s value can be decomposed into several “members” or “elements.”

Each “member” is a basic data type or another constructed type. In C language, there are several types of constructed types:

  • Array type – an array type is actually a type that takes a single data type and turns it into a series of connected data types of the same type.
  • Structure type (Struct) – a structure is another user-defined data type available in C programming that allows you to store data items of different types.
  • Union type – a union is a special data type that allows you to store different data types at the same memory location.
  • Pointer type – points to a memory address.
Related:   Identifiers in C Program Language

3. User-defined Data Types

User-defined data types mainly include enumerations and aliases.

Enumeration (enum)  Data Type – Enumeration is a way to list all the members of a finite sequence set. Enumerations are a basic data type in the C language that make data more concise and readable.

typedef  Data Type – Alias. It is mainly used to define constants.

4. Data Types and Their Memory Bytes

  1. Character type (char): used to store a single character. It takes up 1 byte of memory.
  2. Integer types (int, short, long, long long): used to store whole numbers. The size of these types varies based on the system architecture and compiler, but in most cases, they take up 2, 4, or 8 bytes of memory.
  3. Floating-point types (float, double, long double): used to store decimal numbers. Again, the size of these types varies based on the system architecture and compiler, but typically float takes up 4 bytes, double takes up 8 bytes, and long double takes up 16 bytes of memory.
  4. Void type (void): used to represent the absence of a type. It does not take up any memory.
  5. Boolean type (bool): used to store true or false values. It takes up 1 byte of memory.
  6. Enumerated type (enum): used to define a set of named integer constants. It takes up 2 bytes of memory.
  7. Pointer type (pointer): used to store the memory address of a variable. The size of a pointer depends on the system architecture and compiler, but it is typically 4 or 8 bytes.
  8. Array type (array): used to store a fixed-size collection of values of the same type. The size of an array depends on the number of elements in the array and the size of each element.
  9. Structure type (struct): used to group together variables of different types under a single name. The size of a structure depends on the size of its individual members and any padding added by the compiler for memory alignment.
  10. Union type (union): used to define a variable that may have different types at different times. The size of a union is equal to the size of its largest member.
Related:   The Storages of Integers in Memory, the Character Data Type and Character Variables

Note that the sizes of these data types can vary depending on the system architecture and compiler used. The above sizes are typical, but not guaranteed for every system.

Example 4.1 The Byte Sizes of Various Variables in Memory.

 

#include
int main()
{
    int a = 1;
    char b = 'G';
    double c = 3.14;
    printf("Hello World!\n"); 
        // printing the variables defined
       // above along with their sizes
    printf("Hello! I am a character. My value is %c and my size is %lu byte.\n", b, sizeof(char));
      // can use sizeof(b) above as well
    printf("Hello! I am an integer. My value is %d and my size is %lu bytes.\n", a, sizeof(int));
      // can use sizeof(a) above as well
    printf("Hello! I am a double floating point variable. My value is %lf and my size is %lu bytes.\n",c, sizeof(double));
     // can use sizeof(c) above as well
     printf("Bye! See you soon. :)\n");
    return 0;
}

Output Results:

Leave a Reply