C Data Types Primitive Derived And User Defined

There are the following data types in C language.

Types Data Types
Basic Data Type int, char, float, double
Derived Data Type array, pointer, structure, union
Enumeration Data Type enum
Void Data Type void

Basic Data Types

In the realm of C programming, fundamental data types consist of those rooted in integers and floating points. These integer and floating-point data types can be categorized as either signed or unsigned. The C programming language accommodates both signed and unsigned literals.

The memory allocation of fundamental data types can vary based on whether the system is running on a 32-bit or 64-bit operating system.

Let's explore the fundamental data types. Their sizes are specified based on a 32-bit architecture.

Data Types Memory Size Range
char 1 byte −128 to 127
signed char 1 byte −128 to 127
unsigned char 1 byte 0 to 255
short 2 byte −32,768 to 32,767
signed short 2 byte −32,768 to 32,767
unsigned short 2 byte 0 to 65,535
int 2 byte −32,768 to 32,767
signed int 2 byte −32,768 to 32,767
unsigned int 2 byte 0 to 65,535
short int 2 byte −32,768 to 32,767
signed short int 2 byte −32,768 to 32,767
unsigned short int 2 byte 0 to 65,535
long int 4 byte -2,147,483,648 to 2,147,483,647
signed long int 4 byte -2,147,483,648 to 2,147,483,647
unsigned long int 4 byte 0 to 4,294,967,295
float 4 byte
double 8 byte
long double 10 byte

a) Integer Types (Int)

Whole numbers, which are integer values without fractions or decimals, are categorized under the int data type. Typically, variables like count, indices, and other numerical values utilize the int data type. This data type can hold both positive and negative numbers by default since it is signed. On most devices, an int variable occupies 4 bytes of memory, enabling it to store values within the range of approximately -2 billion to +2 billion.

Signed Integer: This specific data type allocates 4 bytes of memory to accommodate both negative and positive whole numbers. When there is a need to retain such numerical values, the signed integer data type comes into play. In a 32-bit system, it can handle values within the range of -2147483648 to 2,147,483,648.

Example:

Example

signed int x = -20;

An unsigned int is capable of storing solely non-negative integer values, ranging from 0 and upwards, within a 4-byte memory allocation. The allowable range for positive values is double that of a signed integer, spanning from 0 to 4294967295, which is twice the range of signed integers.

Example:

Example

unsigned int x = 18;

Short Integer: When there is a necessity to store integer values that typically require two bytes of memory, the short integer can be employed. In the case of signed short integer values, its range is restricted to -32768 to 32767. This data type proves to be beneficial in memory-restricted scenarios.

Example:

Example

short int a = 1000;

If we need to hold values larger than those accommodated by a standard int, we can opt for the long int data type. Typically, a long int occupies either 4 or 8 bytes of memory. On a 32-bit OS, it can be equated with the int data type for comparison purposes.

Example:

Example

Long int x = 1000000;

C Integer Types Example

Let's consider a scenario to demonstrate the Integer data types in the C programming language.

Example

Example

#include <stdio.h>



int main() {  //main function

    // Integer types

    short int a = 45;         // short int

    unsigned short int b = 7545; // unsigned short int

    int c = 2154455;          // int value

    unsigned int d = 441454581; // unsigned int

    long int e = 24525366552;     // long int

    unsigned long int f = 4294967295; // unsigned long int

    long long int g = 854223755457775807; // long long int

    unsigned long long int h = 8565225254522331615U; // unsigned long long int



    printf("short int a = %hd\n", a);

    printf("unsigned short int b = %hu\n", b);

    printf("int c = %d\n", c);

    printf("unsigned int d = %u\n", d);

    printf("long int e = %ld\n", e);

    printf("unsigned long int f = %lu\n", f);

    printf("long long int g = %lld\n", g);

    printf("unsigned long long int h = %llu\n", h);



    return 0;

}

Output:

In this instance, we explored the various kinds of integers that execute multiple functions in the C programming language.

b) Character Data Type (char)

In the C programming language, the char data type is employed to represent single characters. This data type is commonly utilized for storing characters encoded in ASCII or UTF-8, encompassing alphabetic letters, digits, punctuation marks, or special symbols. A char variable can hold any of the 256 possible characters, occupying one byte of storage. Notably, characters like 'A', 'b', '5', or '$' are enclosed within single quotation marks.

Syntax

It has the following syntax:

Example

char ch = 'A';

C Character Type Example

Let's consider an example to demonstrate the character data type in C programming.

Example

Example

#include <stdio.h>



int main() {  //main function

  char word = 'T';

  printf("%c", word);

  return 0;

}

Output:

c) Float Data Type (Float)

The floating-point data type is utilized for storing numbers with decimal points or fractional values. Floating-point numbers are commonly employed to represent quantities that are not whole numbers. The float data type is typically chosen for variables that need high precision but do not necessarily require absolute accuracy. It is capable of holding values with approximately 6 decimal places of precision and a range of roughly 3.4 x 10^38 within a memory allocation of 4 bytes.

Syntax

It has the following syntax:

Example

float variable_name;

C Float Data Type Example

Let's consider a scenario to demonstrate the float data type in the C programming language.

Example

Example

#include <stdio.h>



int main() {   //main function

  float f_1 = 25.144501;

  printf("%f", f_1);

  return 0;

}

Output:

Output

25.144501

d) Double

Double data types are employed to store floating-point numbers with increased precision, particularly useful in scenarios like scientific computations or financial software where enhanced accuracy is essential compared to float data types.

Double data type, which occupies 8 bytes of memory and offers precision up to approximately 15 decimal places, is suitable for handling larger numerical values. In C programming, floating-point numbers are automatically considered as doubles unless a specific type is specified.

Syntax

It has the following syntax:

Example

double var_name;

C Double Data Type Example

Let's consider an example to demonstrate the double data type in C programming.

Example

Example

#include <stdio.h>



int main() {  //main function

  double mynum = 17.578421;

  printf("%lf", mynum);

  return 0;

}

Output:

Output

17.578421

Derived Data Type

C programming also provides derived data types such as arrays, pointers, structures, and unions. These data types empower developers to manage diverse data, manipulate memory directly, and construct intricate data structures.

a) Array

In a derived data type, an array enables the storage of a series of elements with a consistent size and type. It offers a way to group various instances of identical data under a single identifier.

The index is employed to retrieve the elements within the array, starting at a 0 index for the initial item. The array's size is predetermined upon declaration and remains constant throughout program execution. The array elements are stored in contiguous memory locations.

C Array Example

Let's consider a scenario to demonstrate how to declare and use an array in the C programming language.

Example

Example

#include <stdio.h>

 

int main() { //main function

    int numbers[5]; // Declares an integer array with a size of 5 elements

 

    // Assign values to the array elements

    numbers[0] = 10;

    numbers[1] = 20;

    numbers[2] = 30;

    numbers[3] = 40;

    numbers[4] = 50;

 

    // Display the values stored in the array

    printf("Values in the array: ");

    for (int i = 0; i < 5; i++) {

        printf("%d ", numbers[i]);

    }

    printf("\n");

 

    return 0;

}

Output:

Output

Values in the array: 10 20 30 40 50

b) Pointer

In a derived data type, a pointer is employed to maintain the memory address of another data type. Upon declaring a pointer, the referenced data type is specified initially, followed by the variable name with an asterisk (*) preceding it.

We have the ability to improperly access and modify the content of a variable through pointers by indicating the specific memory location of the variable. Pointers are frequently employed in functions, data structures, and the allocation of dynamic memory.

C Pointer Example

Let's consider an example to demonstrate how a pointer is declared and used in the C programming language.

Example

Example

#include <stdio.h>    



int main() {     // main function

    int num = 54;      // An integer variable    

    int *ptr;          // Declares a pointer to an integer    



    ptr = #        // Assigns the address of 'num' to the pointer    



    // Accessing the value of 'num' using the pointer    

    printf("Value of num: %d\n", *ptr);    



    return 0;    

}

Output:

Output

Value of num: 54

c) Structure

In C programming, a structure is a derived data type that enables the creation of composite data types by allowing the grouping of many data types under a single name. It gives us the ability to create our own unique data structures by fusing together variables of various sorts.

  • A structure's members or fields are used to refer to each variable within it.
  • Any data type, including different structures, can be a member of a structure.
  • A structure's members can be accessed by using the dot (.) operator.

C Structure Example

Let's consider an example to demonstrate how to declare and use structures in the C programming language.

Example

Example

#include <stdio.h>

#include <string.h> 

// Define a structure representing a person

struct Person {

    char name[50];

    int age;

    float height;

};

 

int main() {

    // Declare a variable of type struct Person

    struct Person person1;

 

    // Assign values to the structure members

    strcpy(person1.name, "John Doe");

    person1.age = 30;

    person1.height = 1.8;

 

    // Accessing the structure members

    printf("Name: %s\n", person1.name);

    printf("Age: %d\n", person1.age);

    printf("Height: %.2f\n", person1.height);

 

    return 0;

}

Output:

Output

Name: John Doe

Age: 30

Height: 1.80

d) Union

In C programming, unions provide a way to store different data types in a single memory location, unlike structures where each member occupies its own memory space. Unlike structures, where each member has a separate memory space, members of a union all share a single memory space. A union can hold only one value at a time. Unions are beneficial when there is a need to interchangeably represent multiple data types. Similar to structures, we access union members using the dot (.) operator.

C Union Example

Let's consider an example to demonstrate declaration and initialization in C programming.

Example

Example

#include <stdio.h>

// Define a union representing a numeric value

union NumericValue {

    int intValue;

    float floatValue;

    char stringValue[20];

};

int main() {

    // Declare a variable of type union NumericValue

    union NumericValue value;

    // Assign a value to the union

    value.intValue = 42;

    // Accessing the union members

    printf("Integer Value: %d\n", value.intValue);

    // Assigning a different value to the union

    value.floatValue = 3.14;

    // Accessing the union members

    printf("Float Value: %.2f\n", value.floatValue);

 

    return 0;

}

Output:

Output

Integer Value: 42

Float Value: 3.14

Enumeration Data Type

In the C programming language, developers can use the enumeration data type (enum) to define a series of named constants or enumerators that symbolize a set of related values. Enums allow programmers to assign meaningful names to a set of integer values, enhancing code readability and simplifying maintenance tasks.

C Enumeration Example

Let's use a sample scenario to demonstrate Enumeration Data Types in the C programming language.

Example

Example

#include <stdio.h>

 

// Define an enumeration for days of the week

enum DaysOfWeek {

    Monday,

    Tuesday,

    Wednesday,

    Thursday,

    Friday,

    Saturday,

    Sunday

};

 

int main() {

    // Declare a variable of type enum DaysOfWeek

    enum DaysOfWeek today;

 

    // Assign a value from the enumeration

    today = Wednesday;

 

    // Accessing the enumeration value

    printf("Today is %d\n", today);

 

    return 0;

}

Output:

Output

Today is 2

Void Data Type

In C programming, the void data type is employed to indicate the absence of a specific type. It is commonly used in defining function return types, function parameters, and pointers.

a) Function Return Type

In C programming, a function with a void return type does not yield any value. Instead, it performs a specific task and concludes without passing any data back to the calling function.

Example:

Example

void printHello() { 

printf("Hello, Logic Practice world!\n"); }

b) Function Parameters

In C programming, the void parameter can be employed to indicate that a function does not receive any arguments.

Example:

Example

void processInput(void) {}

c) Pointers

In the C language, any memory address can be stored in a void* pointer, turning it into a versatile pointer. This feature provides a way to handle pointers to uncertain or unconventional data types.

Example:

Example

void* dataPtr;

The void data type is useful for declaring functions that do not require any parameters when dealing with generic pointers or to indicate that a function does not provide a return value.

C Void Example

Let's consider an example that illustrates the usage of void in different scenarios:

Example

Example

#include <stdio.h>

// Function with void return type

void printHello() {

    printf("Hello, world!\n");

}

// Function with void parameter

void processInput(void) {

    printf("Processing input...\n");

}

 

int main() {

    // Calling a void function

    printHello();

 

    // Calling a function with void parameter

    processInput();

 

    // Using a void pointer

    int number = 10;

    void* dataPtr = &number;

    printf("Value of number: %d\n", *(int*)dataPtr);

 

    return 0;

}

Output:

Output

Hello, world!

Processing input...

Value of number: 10

Explanation:

In this instance, we are presenting a printHello function that exhibits a void return type, signifying it doesn't yield any value. Following that, we employ the processInput(void) function that specifies void as a parameter type, denoting it accepts no arguments. The main function further demonstrates a void pointer (void), serving as a universal pointer type for storing any data type's address. Ultimately, the void pointer undergoes typecasting to int for retrieving and displaying the value "iLogic Practices."

Conclusion

In summary, data types play a crucial role in the C programming language by specifying the types of data that variables can store. They determine the size and structure of the data, allowing the compiler to assign memory and perform required operations. C supports various data types such as void, enumeration, derived, and fundamental types.

Input Required

This code uses input(). Please provide values below: