Data Types in C Language

Understanding of Data Types in C Programming Language

In the ever-evolving world of programming, mastering the fundamentals is key to becoming a proficient coder. C data types provide a way to specify the type of data a variable can hold

in C programming. When it comes to the C programming language, understanding data types is an absolute must. In this article, we’ll delve into the world of data types in C, breaking down the essentials and providing you with the knowledge you need to excel in your coding journey.

In the C programming language, data types can be classified into the following categories:

Types Description
Primitive Data TypesThese are the most fundamental data types used to represent basic values like integers, floating-point numbers, and characters.
User Defined Data TypesThese data types are created or defined by the programmer/user themselves, allowing for custom data structures and types.
Derived TypesDerived data types are those that are created based on or derived from the primitive or built-in data types. They build upon the fundamental data types to create more complex data structures.

Various data types can store numbers within specific ranges, and these ranges might differ between different compilers. Below is a list of ranges, memory requirements, and format specifiers for the 32-bit GCC compiler.

Data Type Size (bytes) RangeFormat Specifier 
short int2-32,768 to 32,767%hd
unsigned short int20 to 65,535%hu
unsigned int40 to 4,294,967,295%u
int4-2,147,483,648 to 2,147,483,647%d
long int4-2,147,483,648 to 2,147,483,647%ld
unsigned long int40 to 4,294,967,295%lu
long long int8-(2^63) to (2^63)-1%lld
unsigned long long int80 to 18,446,744,073,709,551,615%llu
signed char1-128 to 127%c
unsigned char10 to 255%c
float41.2E-38 to 3.4E+38%f
double81.7E-308 to 1.7E+308%lf
long double163.4E-4932 to 1.1E+4932%Lf

Here is the provided text rewritten in English:

Primitive Data Types in C:

In the C programming language, there are several fundamental data types that serve as the building blocks for defining variables. These data types include:

Integer Data Type:

The integer data type in C is used to store whole numbers without decimal values. It can accommodate octal values, hexadecimal values, and decimal values.

  • Range: -2,147,483,648 to 2,147,483,647
  • Size: 4 bytes
  • Format Specifier: %d

To declare an integer variable, you use the int keyword:

int var_name;

The integer data type can also be used as:

  • unsigned int: Stores only non-negative values (positive numbers).
  • short int: Smaller in size than int, suitable for values from -32,768 to 32,767.
  • long int: Larger version of int, capable of storing values greater than int.
  • unsigned short int: Similar to short int but stores only non-negative values.

Note that the size of an integer data type depends on the compiler, and you can use the sizeof operator to determine its actual size.

Example of int:

#include <stdio.h>

int main() {
    int a = 9;
    int b = -9;
    int c = 89U;  // U or u is used for unsigned int.
    long int d = 99998L;  // L or l is used for long int.

    printf("Integer value with positive data: %d\n", a);
    printf("Integer value with negative data: %d\n", b);
    printf("Integer value with an unsigned int data: %u\n", c);
    printf("Integer value with a long int data: %ld", d);

    return 0;
}

Character Data Type:

The character data type allows you to store a single character. It occupies 1 byte of memory and is the most basic data type in C.

  • Range: (-128 to 127) or (0 to 255)
  • Size: 1 byte
  • Format Specifier: %c

To declare a character variable, use the char keyword:

char var_name;

Example of char:

#include <stdio.h>

int main() {
    char a = 'a';
    char c;

    printf("Value of a: %c\n", a);

    a++;
    printf("Value of a after increment is: %c\n", a);

    c = 99;  // c is assigned ASCII value corresponding to 'c'.
    printf("Value of c: %c", c);

    return 0;
}

Float Data Type:

In C programming, the float data type is used to store floating-point values, including decimal and exponential values. It provides single precision.

  • Range: 1.2E-38 to 3.4E+38
  • Size: 4 bytes
  • Format Specifier: %f

To declare a float variable, use the float keyword:

float var_name;

Example of Float:

#include <stdio.h>

int main() {
    float a = 9.0f;
    float b = 2.5f;
    float c = 2E-4f;  // 2x10^-4

    printf("%f\n", a);
    printf("%f\n", b);
    printf("%f", c);

    return 0;
}

Double Data Type:

The double data type in C is used for double-precision storage of decimal numbers, providing greater precision compared to float. It can hold 64 bits of decimal values.

  • Range: 1.7E-308 to 1.7E+308
  • Size: 8 bytes
  • Format Specifier: %lf

To declare a double variable, use the double keyword:

double var_name;

Example of Double:

#include <stdio.h>

int main() {
    double a = 123123123.00;
    double b = 12.293123;
    double c = 2312312312.123123;

    printf("%lf\n", a);
    printf("%lf\n", b);
    printf("%lf", c);

    return 0;
}

Void Data Type:

The void data type in C is used to indicate the absence of a value. It does not return a result value to its caller and is used to represent nothing. Void is employed in various ways, such as function return types, function arguments as void, and pointers to void.

Example of Void:

#include <stdio.h>

int main() {
    int val = 30;
    void* ptr = &val;
    printf("%d", *(int*)ptr);

    return 0;
}

Size of Data Types in C:
The size of data types in C depends on the architecture, so there is no universal size. The sizeof() operator is used to determine the size of data types.

Example:

#include <stdio.h>

int main() {
    int size_of_int = sizeof(int);
    int size_of_char = sizeof(char);
    int size_of_float = sizeof(float);
    int size_of_double = sizeof(double);

    printf("The size of int data type: %d\n", size_of_int);
    printf("The size of char data type: %d\n", size_of_char);
    printf("The size of float data type: %d\n", size_of_float);
    printf("The size of double data type: %d", size_of_double);

    return 0;
}

Discover more from PiEmbSysTech

Subscribe to get the latest posts sent to your email.

Leave a Reply

Scroll to Top

Discover more from PiEmbSysTech

Subscribe now to keep reading and get access to the full archive.

Continue reading