What is Data Representation in C Programming ? See Example

Understanding data representation is fundamental to mastering C programming because C interacts closely with machine-level memory. How data is stored, accessed, and interpreted in memory directly affects performance, accuracy, and program behavior.

In this article, we’ll explore how different types of data (numbers, characters, pointers, etc.) are represented inside the computer and how C handles them internally.


1. What Is Data Representation?

Data representation refers to the way information is stored in a computer’s memory using binary digits (bits — 0 or 1). Every integer, character, floating number, or instruction is internally converted into a binary format.

Computers work with:

  • Bits → smallest unit (0 or 1)
  • Bytes → group of 8 bits
  • Words → size depends on architecture (16-bit, 32-bit, 64-bit)

2. Number Systems Used in C

C supports multiple number systems for representing values:

a) Decimal (Base 10)

The numbers we normally use.

int a = 25;

b) Binary (Base 2)

Introduced in C99 (via prefix 0b in some compilers like GCC).

int b = 0b1101; // 13 in decimal

c) Octal (Base 8)

Uses prefix 0.

int c = 075; // 61 in decimal

d) Hexadecimal (Base 16)

Uses prefix 0x.

int d = 0x1A; // 26 in decimal

3. Integer Representation

C typically stores integers using:

  • 8-bit (char)
  • 16-bit (short)
  • 32-bit (int)
  • 64-bit (long long)

Signed vs Unsigned Integers

Signed integers use the most significant bit (MSB) as a sign bit.

Example (8-bit signed integer):

  • Range = –128 to +127
  • Negative numbers use Two’s Complement representation

Two’s Complement Example

Represent –5 in 8 bits:

  1. Write +5 → 00000101
  2. Invert bits → 11111010
  3. Add 1 → 11111011

So, –5 = 11111011

Example Program

#include <stdio.h>

int main() {
    signed char x = -5;
    unsigned char y = -5;

    printf("Signed: %d\n", x);
    printf("Unsigned: %d\n", y);
    return 0;
}

Output will show different values for signed & unsigned.


4. Floating-Point Representation

C follows IEEE-754 standard for representing floating-point numbers.

TypeSizePrecision
float4B~6 digits
double8B~15 digits
long double10–16Barchitecture dependent

Floating-point numbers consist of:

  • Sign bit
  • Exponent
  • Mantissa/Fraction

Example

float a = 5.75;

Binary Representation (simplified):

Sign = 0
Exponent = 10000001
Mantissa = 01110000000000000000000

5. Character Representation (ASCII & Unicode)

C uses ASCII by default for char data type.

Example:

char ch = 'A';
printf("%d", ch); // prints 65

ASCII Codes:

  • A → 65
  • a → 97
  • 0 → 48

Unicode can be used via wchar_t (wide character).


6. Boolean Representation

C doesn’t originally have a built-in boolean type (before C99).
But C treats:

  • 0 → false
  • non-zero → true

In C99:

#include <stdbool.h>

bool flag = true;

7. Pointer Representation

Pointers store memory addresses, typically:

  • 4 bytes in 32-bit systems
  • 8 bytes in 64-bit systems

Example:

int x = 10;
int *p = &x;

printf("Address: %p\n", p);

Output might look like:

Address: 0x7ffeefbff5ac

8. Array Representation in Memory

Arrays store values contiguously:

int arr[3] = {10, 20, 30};

Memory:

Address → Value
1000    → 10
1004    → 20
1008    → 30

Each int takes 4 bytes.


9. Structure Representation

Structure members are stored sequentially, but padding may occur.

struct test {
    char a;   // 1 byte
    int b;    // 4 bytes
};

Memory layout might be:

a (1 byte)
padding (3 bytes)
b (4 bytes)

Total size = 8 bytes (not 5)


10. Endianness (Little vs Big Endian)

Little Endian

Least significant byte stored first
Example (1000 stored as hex 0x000003E8):

E8 03 00 00

Big Endian

Most significant byte stored first:

00 00 03 E8

Example program:

int x = 1;
char *p = (char*)&x;

if (*p == 1) printf("Little Endian");
else printf("Big Endian");

Conclusion

Data representation is the backbone of how C interacts with hardware-level memory. Understanding binary storage, integer formats, floating-point precision, character encoding, and endianness gives you deeper control and helps in debugging, optimization, and system-level programming.


🔗 View other articles about PHP:

https://savanka.com/category/learn/c-programming

🔗 External PHP Documentation:

https://www.w3schools.com/c

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *