TeachingBee

Data Types In C

data types in C

In the C programming language, data types are essential components that define how data is stored and manipulated. They dictate the size of the data, the range of values that can be stored within them, and the set of operations that can be applied to them.

This article will cover the primary data types in C, their sizes, and provide code examples to illustrate their usage.

Different Types Of Data Types In C

data types in c

In C programming, data types can be categorized into several types:

  1. Basic Data Types: These include integer types (char, int, short, long), floating-point types (float, double), and void.
  2. Derived Data Types: Derived data types are created by combining basic data types. These include arrays, pointers, structures, and unions.
  3. Enumerated Data Types (enum): Enumerated data types allow you to define a set of named integer constants.
  4. Void Data Type: A void data type in C represents the absence of type information. It indicates that no value is available. Void Data Type is a keyword in the C language that represents the idea of “nothing” or “no type“.
  5. User-defined Data Types: Data types that are defined by the user using typedef keyword. They can be based on any of the existing data types.

These categories encompass all the data types available in C programming, each serving different purposes and offering various levels of abstraction and flexibility.

Let’s look into each one in detail.

Basic Data Types in C

C offers a variety of basic data types, which can be broadly classified into three categories:

  1. integer types
  2. floating-point types
  3. char type.

Integer Types

Integer types are used to store whole numbers, both positive and negative. They come in various forms, each with a specific size and range.

Signed Integer Types

Signed integer types in C can represent both positive and negative numbers. They are characterized by the inclusion of a sign bit, allowing them to express values ranging from -(2^(N-1)) to (2^(N-1))-1, where N is the number of bits used to represent the integer type.

Data TypeSize (bytes)Range
short int2-32,768 to 32,767
int4-2,147,483,648 to 2,147,483,647
long int4-2,147,483,648 to 2,147,483,647
long long int8-(2^63) to (2^63)-1

Unsigned Integer Types

Unsigned integer types in C represent only non-negative numbers, including zero. They do not include a sign bit, allowing them to use the full range of their allocated bits to represent positive values.

Data TypeSize (bytes)Range
unsigned short int20 to 65,535
unsigned int40 to 4,294,967,295
unsigned long int40 to 4,294,967,295
unsigned long long int80 to 18,446,744,073,709,551,615

Floating-Point Types

Floating-point types are used to store real numbers (i.e., numbers with decimal points).

Data TypeSize (bytes)Range
float4Approximately 1.2E-38 to 3.4E+38
double8Approximately 1.7E-308 to 1.7E+308
long double16Approximately 3.4E-4932 to 1.1E+4932

char Types

In C, the char type is used to store single characters, typically represented as ASCII values. It occupies 1 byte of memory and can hold characters ranging from -128 to 127 or 0 to 255, depending on whether it’s signed or unsigned.

Here’s the range of char type in tabular format:

Data TypeSize (bytes)Range
signed char1-128 to 127
unsigned char10 to 255

C Program for Usage Of Basic Data Types In C

Let’s examine some code examples to understand how these data types are used in practice.

C Program for Usage of Integer Types

#include <stdio.h>

int main() {
    // Declare and initialize variables of different integer types
    short int si =  32767;
    int i =  2147483647;
    long int li =  2147483647L;
    long long int lli =  9223372036854775807LL;

    printf("Short int: %hd\n", si);
    printf("Int: %d\n", i);
    printf("Long int: %ld\n", li);
    printf("Long long int: %lld\n", lli);

    return  0;
}

C Program for Usage of Floating-Point Types

#include <stdio.h>

int main() {
    // Declare and initialize variables of different floating-point types
    float f =  3.14F;
    double d =  2.71828;
    long double ld =  1.234567890123456789L;

    printf("Float: %.2f\n", f);
    printf("Double: %.2lf\n", d);
    printf("Long double: %.2Lf\n", ld);

    return  0;
}

Basic Data Types In C With Size

It’s important to note that the actual size of these data types can vary depending on the system architecture and compiler being used. However, for a typical 32-bit GCC compiler, the sizes are as follows:

Data TypeSize (bytes)
short int2
int4
long int4
long long int8
float4
double8
long double16

You can verify the size of a data type using the sizeof operator:

#include <stdio.h>

int main() {
    printf("Size of int: %zu bytes\n", sizeof(int));
    printf("Size of float: %zu bytes\n", sizeof(float));
    printf("Size of double: %zu bytes\n", sizeof(double));
    printf("Size of long double: %zu bytes\n", sizeof(long double));

    return  0;
}

Derived Data Types in C

In addition to the basic data types, C offers several derived data types that allow for more complex data structures and manipulations. These include arrays, structures, unions, pointers, and user-defined types like enumerations and void.

Arrays In C

Arrays are collections of elements of the same data type, stored in contiguous memory locations. Each element in an array can be accessed by its index.

int numbers[5] = {1,  2,  3,  4,  5}; // An array of  5 integers

Pointers

Pointers store the memory address of another variable. They are powerful tools for dynamic memory management.

int x =  10;
int *ptr = &x; // ptr now holds the address of x

Structures (struct)

Structures allow the grouping of variables of different data types under a single name. This is particularly useful for modeling complex entities.

struct Student {
    char name[50];
    int age;
    float gpa;
};
struct Student s1;

Unions In C

Unions are similar to structures, but they share the same memory location for storing their members. Only one member can hold a value at a time.

union Data {
    int intValue;
    float floatValue;
    char stringValue[20];
};
union Data data1;

Here are some examples demonstrating the use of these derived data types:

C Program for Usage Of Derived Data Types In C

C Program for Usage of a Arrays

#include <stdio.h>

int main() {
    int numbers[] = {1,  2,  3,  4,  5};
    int length = sizeof(numbers) / sizeof(numbers[0]);

    for (int i =  0; i < length; i++) {
        printf("%d ", numbers[i]);
    }

    return  0;
}

C Program for Usage of a Pointer

#include <stdio.h>

int main() {
    int num =  100;
    int *numPtr = #

    printf("Value of num: %d\n", num);
    printf("Address of num: %p\n", &num);
    printf("Value of numPtr: %p\n", numPtr);
    printf("Value pointed by numPtr: %d\n", *numPtr);

    return  0;
}

C Program for Usage of a Structure

#include <stdio.h>

struct Student {
    char name[50];
    int age;
    float gpa;
};

int main() {
    struct Student s1;

    strcpy(s1.name, "John Doe");
    s1.age =  20;
    s1.gpa =  3.8;

    printf("Name: %s\n", s1.name);
    printf("Age: %d\n", s1.age);
    printf("GPA: %.2f\n", s1.gpa);

    return  0;
}

C Program for Usage of a Union

#include <stdio.h>

union Data {
    int intValue;
    float floatValue;
    char stringValue[20];
};

int main() {
    union Data data;

    data.intValue =  10;
    printf("intValue: %d\n", data.intValue);

    data.floatValue =  220.5;
    printf("floatValue: %.2f\n", data.floatValue);

    strcpy(data.stringValue, "C Programming");
    printf("stringValue: %s\n", data.stringValue);

    return  0;
}

Enumerations (enum)

Enumerations are user-defined data types that consist of a set of named integer constants. They are useful for creating readable code and can serve as flags.

#include <stdio.h>

enum Days {SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY};

int main() {
    enum Days today;

    today = FRIDAY;
    printf("Today is day number %d\n", today);

    return  0;
}

Derived Enumerated Data Types In C

Derived Enumerated Data Types in C are constructed by pairing basic data types with enumerations, allowing programmers to define custom data types with predefined sets of named integer constants.

This association facilitates the creation of data types tailored to specific application requirements, enhancing code clarity, and maintainability.

// Example of a derived enumerated data type
enum Weekday {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday};
typedef enum Weekday Day;

Void Data Type In C

The void data type represents the absence of a type. It is primarily used for functions that do not return any value.

void printMessage() {
    printf("Hello, World!");
}

To know more in detail check out our previous post on Void Data Type In C.

User-defined Data Types In C

User-defined Data Types in C are crafted by programmers utilizing the typedef keyword, granting them the ability to create customized aliases for existing data types.

By encapsulating complex or frequently used data types within user-defined aliases, developers can simplify code comprehension and streamline future modifications. Additionally, typedefs promote consistency across the codebase, as they enable developers to employ descriptive names consistently throughout their programs.

// Example of a user-defined data type
typedef int Counter;
Counter count = 0;

Key TakeAways

  • C offers basic data types like integers, floating-points, and void.
  • Integer types include signed and unsigned varieties, covering ranges from short int to long long int.
  • Floating-point types accommodate real numbers with varying precision levels: float, double, and long double.
  • Derived data types such as arrays, structures, unions, and pointers facilitate complex data structures and dynamic memory management.
  • Enumerations allow the creation of custom sets of named integer constants, enhancing code readability and maintainability.

Similar Posts

Union In C

Arrays In C

Void Data type In C

Strings In C

Also check,

Checkout more C Tutorials here.

I hope You liked the post 👍. For more such posts, 📫 subscribe to our newsletter. Try out our free resume checker service where our Industry Experts will help you by providing resume score based on the key criteria that recruiters and hiring managers are looking for.

FAQ

What are the data types in C?

What are the 5 different data types?

What is the char type in C?

What is byte type in C?

90% of Tech Recruiters Judge This In Seconds! 👩‍💻🔍

Don’t let your resume be the weak link. Discover how to make a strong first impression with our free technical resume review!

Related Articles

Subtraction of Two Numbers Program in C

In this blog post, we will checkout the Subtraction of Two Numbers Program in C. How to Write Subtraction Program in C? To create a C program to subtract two

Add Two Numbers In C

In this blog post, we will checkout the how to add two numbers in C. Algorithm to Add Two Numbers in C To create a C program to add two

Why Aren’t You Getting Interview Calls? 📞❌

It might just be your resume. Let us pinpoint the problem for free and supercharge your job search. 

Newsletter

Don’t miss out! Subscribe now

Log In