DataType in C

A Data Type in C programming defines the type, format, and nature of data that a variable can hold, as well as the exact number of bytes reserved in computer memory (RAM). The choice of an appropriate data type allows the programmer to optimize memory consumption and execution speed according to the requirements of the application and the host processor architecture.

The Three Major Classes of Data Types in C

In C programming, data types are broadly classified into three categories:

 1. Primary (Fundamental / Built-in) Data Types: int, char, float, double, void.

 2. Derived Data Types: Constructed from primitive types: arrays, pointers, functions.

 3. User-Defined Data Types: Defined by the programmer: struct, union, enum, typedef.

Primary Data Types, Sizes, Ranges & Format Specifiers

Data type modifiers — signed, unsigned, short, long — alter the size and signedness of primitive types:

Data Type

Size (Typical)

Value Range

Format Specifier

char or signed char

1 Byte (8 bits)

-128 to 127

%c

unsigned char

1 Byte (8 bits)

0 to 255

%c or %u

short int

2 Bytes (16 bits)

-32,768 to 32,767

%hd

int

4 Bytes (32 bits)

-2,147,483,648 to 2,147,483,647

%d or %i

unsigned int

4 Bytes (32 bits)

0 to 4,294,967,295

%u

long int

4 or 8 Bytes

-2^63 to 2^63 - 1

%ld

float

4 Bytes (32 bits)

1.2E-38 to 3.4E+38 (6 dec precision)

%f

double

8 Bytes (64 bits)

2.3E-308 to 1.7E+308 (15 dec precision)

%lf

long double

10, 12, or 16 Bytes

3.4E-4932 to 1.1E+4932 (19 dec precision)

%Lf

void

0 Bytes

Represents absence of value or untyped memory

N/A

Working Example: Inspecting Byte Sizes Using sizeof

Because the exact byte size of int and pointers can vary between 16-bit (Turbo C), 32-bit, and 64-bit platforms, C provides the compile-time sizeof operator:


#include <stdio.h>

int main()
{
    printf("--- Memory Footprint of C Data Types ---\n");
    printf("Size of char        : %zu byte(s)\n", sizeof(char));
    printf("Size of int         : %zu byte(s)\n", sizeof(int));
    printf("Size of float       : %zu byte(s)\n", sizeof(float));
    printf("Size of double      : %zu byte(s)\n", sizeof(double));
    printf("Size of long int    : %zu byte(s)\n", sizeof(long int));
    printf("Size of void* (ptr) : %zu byte(s)\n", sizeof(void*));

    return 0;
}
							

Type Conversion & Type Casting in C

When an operation involves multiple data types, C handles the conversion in one of two ways:

 Implicit Type Conversion (Coercion): Performed automatically by the compiler following standard type promotion rules without loss of precision (e.g., in int + float, the integer is promoted to float).

 Explicit Type Casting: Explicitly forced by the programmer using cast syntax: (target_type) expression.


int a = 7, b = 2;
float result;

result = a / b;             /* Integer division: result = 3.000000 */
result = (float) a / b;     /* Explicit cast: result = 3.500000 */
							

Practice Session for Beginner Students

Sharpen your understanding of data representation, ranges, and format conversions.

Part 1: Spot the Bug & Predict the Output

Problem 1: What happens in the following code on a system where char is signed (range -128 to 127)?


#include <stdio.h>
int main() {
    char ch = 127;
    ch = ch + 1;
    printf("%d", ch);
    return 0;
}
							

Answer & Explanation: Output is -128! This is a classic Integer Overflow bug. Adding 1 to the maximum positive signed char (127 or binary 01111111) flips the sign bit to 10000000, representing -128 in two's complement representation.

Part 2: Multiple Choice Questions (BCA Exam Preparation)

Q1. What is the format specifier for a double in printf() and scanf()?
A) %d
B) %lf
C) %f
D) %c
Correct Answer: B) %lf (Long float).

Q2. What is the size of an int in standard 16-bit Turbo C vs modern 64-bit GCC compilers?
A) 2 bytes in Turbo C, 4 bytes in modern GCC
B) 4 bytes in Turbo C, 8 bytes in modern GCC
C) Always 4 bytes
D) 1 byte in Turbo C, 2 bytes in GCC
Correct Answer: A) 2 bytes in Turbo C, 4 bytes in modern GCC.

Q3. What does (float) 5 / 2 evaluate to?
A) 2.0
B) 2.5
C) 2
D) 3
Correct Answer: B) 2.5 (Explicit type casting forces floating-point division).

Part 3: Hands-on Lab Assignments for Beginners

Assignment 1: Write a C program to input a single character from the keyboard and print its corresponding ASCII integer value using %d.
Logic Hint: char ch; scanf("%c", &ch); printf("ASCII = %d", ch);.

Part 4: Frequently Asked University Viva Questions

1. What is the difference between float and double?
Answer: float occupies 4 bytes of memory and provides approximately 6 decimal places of precision (single precision). double occupies 8 bytes and provides roughly 15 decimal places of precision (double precision).

2. What is a void data type and where is it used?
Answer: void signifies “no type” or “empty value”. It is used as a function return type when no value is returned, as an empty parameter list (int main(void)), or to declare generic pointers (void *ptr).

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!