C Data Types: The Ultimate Beginner’s Guide

In C programming, data types define the type of data a variable can store. Choosing the right data type is essential for efficient memory use, program accuracy, and performance.

Let’s explore the built-in, derived, and user-defined data types in C with examples and tips.

Basic (Primary) Data Types

Data TypeDescriptionSize (Typical)Example
intInteger2 or 4 bytesint age = 30;
floatSingle-precision float4 bytesfloat pi = 3.14;
doubleDouble-precision float8 bytesdouble x = 10.9;
charCharacter1 bytechar grade = 'A';

Example Program:

#include <stdio.h>

int main() {
    int age = 25;
    float pi = 3.14;
    double largeValue = 123456.789;
    char initial = 'M';

    printf("Age: %d\n", age);
    printf("PI: %.2f\n", pi);
    printf("Large Value: %.3lf\n", largeValue);
    printf("Initial: %c\n", initial);

    return 0;
}

Derived Data Types

These are based on the basic data types:

TypeDescriptionExample
ArrayCollection of same typeint numbers[5];
PointerAddress of a variableint *ptr = &age;
FunctionReturns a specific typeint sum(int a, int b)

User-defined Data Types

You can define your own types using:

KeywordUseExample
structGroup different data typesstruct Person { char name[50]; int age; };
unionShared memory for membersunion Data { int i; float f; };
enumNamed integer constantsenum Color { RED, GREEN, BLUE };
typedefCreate aliases for typestypedef int Marks;

Size of Data Types (Platform Dependent)

Data TypeSize (32-bit system)
char1 byte
int4 bytes
float4 bytes
double8 bytes

Use sizeof() to check data type size:

printf("%lu", sizeof(int));

Best Practices for Data Types

  • Use int for counting, float/double for real numbers
  • Use char for characters, not strings
  • Use the smallest data type that fits your needs
  •  Group related variables using struct
  • Prefer enum for readable constants

Common Mistakes to Avoid

MistakeWhy It’s a Problem
Using wrong data typeCan waste memory or cause errors
Not checking type sizesLeads to portability issues
Mixing types without castingMay cause unexpected results

Read More

How to Create Database in MySQL

  • By admin
  • November 27, 2021
  • 65 views
How to Create Database in MySQL

How to create table in MySQL

  • By admin
  • November 6, 2021
  • 46 views
How to create table in MySQL

MySQL commands with examples

  • By admin
  • September 11, 2021
  • 139 views
MySQL commands with examples

MySQL use database

  • By admin
  • May 28, 2021
  • 45 views
MySQL use database

System Software

  • By admin
  • May 20, 2021
  • 66 views

Introduction to software

  • By admin
  • May 13, 2021
  • 58 views
Introduction to software