C Data Types

In the C programming language, a data type is a classification that specifies which type of value a variable can hold and what kind of operations can be performed on that variable. C provides a variety of built-in data types.

1. Basic Data Types

Integer Types:

int

The int data type is used to store whole numbers (both positive and negative) without any decimal points.

size: It has 4 bytes

range: It has range from -2,147,483,648 to 2,147,483,647.

short

The short data type is used to store smaller integer values, typically requiring less memory than the standard int.

size: It has typically 2 bytes(16 bits)

Range: It has range from -32,768 to 32,767

long

The long data type is used to store larger integer values than the standard int.

size: Typically 4 bytes (32 bits) on 32-bit systems, and 8 bytes (64 bits) on 64-bit systems.

Range: Typically from -2,147,483,648 to 2,147,483,647 for a signed long on a 32-bit system

long long

The long long data type is used to store even larger integers than long. It is guaranteed to be at least 8 bytes in size.

Example:


#include <stdio.h>

int main() {
    int x = 30;              // Regular integer
    short y = 4;             // Short integer (smaller size)
    long z = 123456789L;     // Long integer
    long long a = 9876543210LL;  // Long long integer

    printf("int x: %d\n", x);
    printf("short y: %d\n", y);
    printf("long z: %ld\n", z);
    printf("long long a: %lld\n", a);
    return 0;
}

Output:

int x: 30
short y: 4
long z: 123456789
long long a: 9876543210

Character Type:

char

The char data type is used to store a single character (such as a letter or a symbol). Internally, it holds an integer value corresponding to the ASCII value of the character.

Size: Typically 1 byte (8 bits).

Range: For signed char, the range is from -128 to 127, and for unsigned char, it is from 0 to 255.

Example:


#include <stdio.h>

int main() {
    char firstCharacter = 'A';  // Single character

    printf("first Character: %c", firstCharacter);

    return 0;
}

Output:

first Character: A

Floating-Point Types:

It is used for storing decimal numbers.

float

The float data type is used to store single-precision floating-point numbers (i.e., numbers with decimal points). It has typically 4 bytes.

double

The double data type is used to store double-precision floating-point numbers, offering more precision and a larger range than float. It has Typically 8 bytes (64 bits).

Example:


#include <stdio.h>

int main() {
    float a = 20.10f;           // Single precision floating-point
    double b = 5.123456789;  // Double precision floating-point
    long double c = 4.718281828459045L;  // Long double precision

    printf("float a: %.2f\n", a);
    printf("double b: %.15f\n", b);
    printf("long double c: %.15Lf\n", c);

    return 0;
}

Output:

float a: 20.10
double b: 5.123456789000000
long double c: 4.718281828459045

2. Void Type

It is used to specify that a function does not return a value or that a pointer does not point to any specific data type.


#include <stdio.h>

int main() {
     void printWelcome(void) {
        printf("Hello, techfliez.com");
    }

   printWelcome();
    return 0;
}

Output:

Hello, techfliez.com

3. Derived Data Types

Array

An array is a collection of elements of the same type, stored in contiguous memory locations.

Example:


int numbers[5] = {10, 20, 30, 40, 50}; //Array of numbers
char name[] = "John";  // Array of characters (string)

Pointer

A pointer is a variable that stores the memory address of another variable.

Example:


int number = 10;
int *numberPtr = &number;  // storing the address of 'number' to numberPtr

Structure

A structure is a user-defined data type that groups variables of different types under a single name.

Example:


struct Employee {
    char name[30];
    int age;
};

struct Employee emp1= {"John", 35};

Union

A union is a special data type in C that allows you to store different types of data in the same memory location. This means that a union can store only one value at a time, which is the value of the member that was most recently assigned.

The size of a union is determined by the largest member of the union, and it uses that amount of memory.

Example:


#include <stdio.h>
#include <string.h>

union Data {
    int a;
    float b;
    char c[30];
};

int main() {
    // Creating a union variable
    union Data data;

    // Storing an integer
    data.a = 50;
    printf("data.a: %d\n", data.a);

    // Storing a float (this will overwrite the integer value)
    data.b = 3.14;
    printf("data.b: %.2f\n", data.b);

    // Storing a string (this will overwrite the float value)
    // We can store only one member's value at a time.
    // The string will be stored in the same memory location as the float.
    strcpy(data.c, "Hello, World!");
    printf("data.c: %s\n", data.c);

    // Note: Only one value is valid at a time, the others are overwritten.
    return 0;
}

Output:

data.a: 50
data.b: 3.14
data.c: Hello, World!

C Data Types – Interview Questions

Q 1: What are data types in C?
Ans: Data types specify the type and size of data a variable can store.
Q 2: Name the basic data types in C.
Ans: int, float, char, and double.
Q 3: What is the use of sizeof operator?
Ans: It returns the memory size of a data type or variable.
Q 4: What is the difference between float and double?
Ans: double provides more precision than float.
Q 5: What are derived data types in C?
Ans: Arrays, pointers, structures, and unions.

C Data Types – Objective Questions (MCQs)

Q1. Which of the following is not a basic data type in C?






Q2. What is the size of an int data type (in most 32-bit compilers)?






Q3. Which data type is used to store a single character in C?






Q4. Which of the following format specifiers is used for float data type in C?






Q5. Which data type would you use to store a large number like 9876543210 in C?






Related C Data Types Topics